? order of execution / or who done what to whom and when ?

Hello ,
I am having trouble with promises ,
or who done what to whom and when ?
Pls , what is the order of execution with the following code:

const wait = time => new Promise((resolve) => setTImeout(resolve, time)) ;

wait(3000).then(() => console.log('Hello!')) ;

Is this correct

  1. const wait = time
  2. Promise((resolve)
  3. setTImeout(resolve, time)
  4. wait(3000).then(()
  5. console.log(‘Hello!’)
    When did 3000 get plugged into time ?

Also any chance you could convert code into (es4) type callback code . Maybe then I could understand it better .
Thanks

This might help, I tried to make it like older JS.

  // declare const wait  
const wait = function (milliseconds){
//declare setTimeout
        function setTimeout(milliseconds){set browser timer; wait milliseconds; return alarm;}
//call setTimeout
  setTimeout(3000); //-> then if/ else
        if(browser timer works){ Promise resolved; return callback .then(value 1)
        // pass in the return value  N/A for setTimeout}
        else{ Promise rejected; return callback .catch(error) }
        }
//call const timer value 3000
   timer(3000) //then pass in return value of timer or nothing to .then
        .then(new function(value 1){run something(value 1); return callback .then(value 2)})
        .then(new function (value 2) {run something(value 2); return .then(value 3)})
        .then(new function (value 3){ run something(value 3); return final value})
        .catch(function (error){alert('this function has blown up because the browser timer is broken or a .then has thrown an error')}

I could try to explain it a different way if that doesn’t make sense.
P.S. the ‘new function’ in the .then’s is just to emphasize that it is a callback and not just nesting the .then’s inside wait.

Sorry I have probably simplified it too much,
this is a bit closer to how it works:

const wait = function (milliseconds){

    //Promise runs whatever is passed in e.g. reject is optional

   return  function new Promise(resolve, reject, setTimeout/callback/or inline){ 

            function setTimeout(milliseconds){
                set browser timer; 
                wait milliseconds; 
                // pass in the return value  N/A for setTimeout}
                if(browser timer works){ Promise resolved; return callback .resolve(value 1)-> "then(value 1)"}
                else{ Promise rejected; return callback .reject(error)-> '.catch(error)' }
            }

            function resolve(value){return .then(value) || return value from new Promise } ;

            function reject(error){return .catch(error) || return error from new Promise}
        }    

     setTimeout(milliseconds) ;

     return resolve value || reject error  

    }
}

            //call setTimeout
      setTimeout(3000); //-> then if/ else
    //call const timer value 3000
       timer(3000) //then pass in return value of timer or nothing to .then
            .then(new function(value 1){run something(value 1); return callback .then(value 2)})
            .then(new function (value 2) {run something(value 2); return .then(value 3)})
            .then(new function (value 3){ run something(value 3); return final value})
            .catch(function (error){alert('this function has blown up because the browser timer is broken or a .then has thrown an error')}

Sorry I didn’t realize I hadn’t replied to you yet .
Thank you very much for your Help…
I understand things better now .

2 Likes

No worries, I edited it a fair bit to try and convert how I think about promises to how you probably do without over complicating it or over simplifying it. Some of it got wiped in the upgrade but as long it helps :slightly_smiling_face: