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

**URL:** <https://forum.kirupa.com/t/order-of-execution-or-who-done-what-to-whom-and-when/646505>\
**Category:** programming\
**Created:** [March 8, 2021, 10:30pm UTC](https://forum.kirupa.com/t/order-of-execution-or-who-done-what-to-whom-and-when/646505 "2021-03-08T22:30:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmars316](https://avatars.discourse-cdn.com/v4/letter/v/bcef8e/32.png) [@vmars316](https://forum.kirupa.com/u/vmars316)\
**Post date:** [March 8, 2021, 10:30pm UTC](https://forum.kirupa.com/t/order-of-execution-or-who-done-what-to-whom-and-when/646505/1 "2021-03-08T22:30:10Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![steve.mills](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/steve.mills/32/14961_2.png) [@steve.mills](https://forum.kirupa.com/u/steve.mills)\
**Post date:** [March 9, 2021, 12:29am UTC](https://forum.kirupa.com/t/order-of-execution-or-who-done-what-to-whom-and-when/646505/2 "2021-03-09T00:29:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![steve.mills](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/steve.mills/32/14961_2.png) [@steve.mills](https://forum.kirupa.com/u/steve.mills)\
**Post date:** [March 9, 2021, 5:43am UTC](https://forum.kirupa.com/t/order-of-execution-or-who-done-what-to-whom-and-when/646505/3 "2021-03-09T05:43:01Z")

</div>

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')}
```

---

<div class="post-metadata">

**Author:** ![vmars316](https://avatars.discourse-cdn.com/v4/letter/v/bcef8e/32.png) [@vmars316](https://forum.kirupa.com/u/vmars316)\
**Post date:** [March 14, 2021, 1:11am UTC](https://forum.kirupa.com/t/order-of-execution-or-who-done-what-to-whom-and-when/646505/4 "2021-03-14T01:11:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![steve.mills](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/steve.mills/32/14961_2.png) [@steve.mills](https://forum.kirupa.com/u/steve.mills)\
**Post date:** [March 15, 2021, 7:08am UTC](https://forum.kirupa.com/t/order-of-execution-or-who-done-what-to-whom-and-when/646505/5 "2021-03-15T07:08:02Z")

</div>

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 🙂
