Jump to content
Sign in to follow this  

Promises in Javascript

Recommended Posts

Hello guys

I have been learning and using Javascript for sometime now I have met a situation where I have  3 functions, where one has to wait for one another to finish executing the after they finished executing I have to go to some block of code. This 3 functions take some time to execute as they even have some database queries so they become left out so I heard thios can be fixed by promises but please help how can I use promises in this situation. Make simple code snippents please.


//create array of events

event = [];


//first function

go to shop()

    if gone to shop is true

           add to array of events that I have gone to shop


// second function

buy the shoes()

       if bought shoes is true

                 add to array of event that I have bought shoes

//third function

come back home()

     if came back to home is true

          add to array of events that came back


//then check if array of events is empty

if(eventsArray.lenght > 0)

     return that some function came true


     return none succeded




Edited by fezekile
Was not complete

Share this post

Link to post
Share on other sites

Why not just wrap that last block of code in a function that you call whenever you're trying to check for events?

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...