# How do promises program flow work in javascript?

**URL:** https://forum.kirupa.com/t/how-do-promises-program-flow-work-in-javascript/655596
**Category:** web dev
**Created:** [December 19, 2022, 12:09pm UTC](https://forum.kirupa.com/t/how-do-promises-program-flow-work-in-javascript/655596 "2022-12-19T12:09:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)
#### Post date: [December 19, 2022, 12:09pm UTC](https://forum.kirupa.com/t/how-do-promises-program-flow-work-in-javascript/655596/1 "2022-12-19T12:09:27Z")

</div>

**Promises syntax that I learnt:**

```auto
let p = new Promise(function (resolve, reject) {
  let x = 20, y = 20;
  if (x == y) {
    resolve();

  }
  else {
    reject();
  }
})

p.then(function () {
  console.log("that's correct");
})

  .catch(function () {
    console.log("that's not correct");
  })

```

I don’t even understand what happens here, the flow control of here. My mild guess is when resolve is called .then part happens, and when reject is called .catch part happens.

I’ve like read countless articles, banged my head against the wall but it’s still not getting inside my head. I feel so dumb for asynchronous javascript.

Please help me understand the program flow step by step in more detail.

---

<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: [December 19, 2022, 8:22pm UTC](https://forum.kirupa.com/t/how-do-promises-program-flow-work-in-javascript/655596/2 "2022-12-19T20:22:07Z")

</div>

Hey mate,  
TBH I think Promise syntax is a complicated way of teaching Promises and `new Promise` also makes it more confusing 🙂

How about we try `async await`…

// 1. Request new emails  
// 2. Pause the function at the `await` and let other code run  
// 3. Save the result to new\_Mail and resume the function when the emails are received  
// 4. Pause the function return at the `await` until emails converted to JSON  
// 5. return the decoded emails  
//6. If the internet/ wifi disconnects `catch` the error and return an error object to the other code that called the function

```auto
async function refresh_Mail( ){
  try{
     let new_Mail = await fetch(“email.com”)
     let decoded = await JSON.parse(new_Mail)
     return decoded
    }
   catch{ return { status: “offline”, body: {}}
   }
}

```

So every time you see the `await` word the function will pause while something else is being done (network, parsing ect) and the `let` variable is storing a `new Promise` that automatically rejects if theres an error…. which will be caught be `catch`…

---

<div class="post-metadata">

### Author: ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)
#### Post date: [December 20, 2022, 3:11am UTC](https://forum.kirupa.com/t/how-do-promises-program-flow-work-in-javascript/655596/3 "2022-12-20T03:11:18Z")

</div>

thank you.
