DEV Community

Cover image for Day 43 of #100DaysOfCode: Review how to use async/await for the Promise (ES7)
Jen-Hsuan Hsieh
Jen-Hsuan Hsieh

Posted on • Edited on

3

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!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay