Day 43 of #100DaysOfCode: Review how to use async/await for the Promise (ES7)

jenhsuan

Jen-Hsuan Hsieh

Posted on November 3, 2020

Day 43 of #100DaysOfCode: Review how to use async/await for the Promise (ES7)

Introduction

Aync/await is used to resolve Promise-based objects instead of using then function

Now we try to improve the example in Day42. The left column is the traditional way to resolve the Promise object. The right column is using wait

1. Resolving a Promise object by using await

  1. We are only allowed to use await in async function.
  2. The program will proceed to next line of code after completing.
  3. We have to add try catch around the code to handle error.

Alt Text

2. Resolving multiple Promise objects at once by using await (Promise.all)

Alt Text

  • Check the state of the Promise.all object. The result is rejected if one of resolved objects has been rejected.

Alt Text

3.Resolving multiple Promise objects at once in different states by using await (Promise.allSettled)

Alt Text

  • Check the state of the Promise.all object. It will contain all results of elements

Alt Text

Articles

There are some of my articles. Feel free to check if you like!

💖 💪 🙅 🚩
jenhsuan
Jen-Hsuan Hsieh

Posted on November 3, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related