home..

How to explain promises and Async/Await in layman's terms

You wanted to buy some oranges. You went to a shop.

 let myPromise = new Promise(function(resolve, reject) {
  setTimeout(function() { resolve("oranges!!"); }, 3000);
});

After several hours, you may have got the oranges

Staff didn’t care what you are going to do with the oranges. So you took them out of the shop, and then did whatever you like with them.

If you want to eat them raw (finish using the data), there will be no more return. Your task can stop here.

myPromise.then((value)=>{
  document.getElementById("demo").innerHTML = value;
},(err)=>{});

But you wanted to take them to a cake shop, and use them as materials to make a cakes.

Then you went to the second shop

The second shop gave you another device with two lights.(then returns a promise as well)

myPromise
.then((oranges)=>{
  //making a cake
  return cake;
})
.then((cake)=>{console.log(cake)})
.catch((err)=>{});

Then you can go to the third shop, the forth shop. That is called Promise Chain

Promise chain is not elegant enough?

That’s what Async/Await for. Async/Await is just a syntax sugar. Async is for packing the return of a normal function into a Promise.

async fn(){}

equals to

async fn(){
  return Promise.resolve(undefined);
}
async fn(){
  let myPromise = new Promise(function(resolve, reject) {
    setTimeout(function() { resolve("oranges!!"); }, 3000);
  });
}

equals to the staff informs you oranges are ready for pick up.

Then you can pick them up:

let oranges = await myPromise;

equals to

.then((oranges)=>{})

Just be mindful of where to put await. Await has to be within the async.

async fn(){
  try{
    let orangePromise = new Promise(function(resolve, reject) {
      setTimeout(()=> { resolve("oranges!!"); }, 3000);
    });
    let oranges = await orangePromise;

    let cakePromise = new Promise(function(resolve, reject) {
      setTimeout(()=> { 
        //do something with oranges
        resolve("cake!!"); }, 3000);
    });
    let cake = await cakePromise;
  }
  catch(err){}
}
© 2023 Diana Wenjiao Liu   •  Powered by Soopr   •  Theme  Moonwalk