Write promises chaining using async/await.

async and await are JavaScript keywords that enable us to work with promises in a more simple way.

async

If you define a function that returns a promise, and this promise will always only resolve, in this case, instead of constructing a pro…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Ayanabilothman

async and await are JavaScript keywords that enable us to work with promises in a more simple way.

async

If you define a function that returns a promise, and this promise will always only resolve, in this case, instead of constructing a promise, you can use async keyword before the function and directly return the resolved value.

Let's clarify by an example 👇

Image description

You can replace the second function with the first one. Using async keyword before the function will make this function return an implicit promise behind the scenes. The value you actually return from this function will act as if it is a result passed through hidden resolve function.

And in turns, we can use chaining on the returned promise.

Image description

What about await keyword ?

await

await keyword is used instead of then function, which means we can let go of writing chains.

Image description

Be careful about 🤓

  1. await keyword must be used before promises, you can't await for anything expect promise.

  2. await keyword must be used inside a function with async keyword.

Summary:

  • async replaces constructing promises.
  • await replaces then.
  • async can be used alone, but await must be used inside async function.

Error handling of promises is a great topic. It will be clarified in another article, just wait for it 🔥.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Ayanabilothman


Print Share Comment Cite Upload Translate Updates
APA

Ayanabilothman | Sciencx (2023-02-11T21:17:09+00:00) Write promises chaining using async/await.. Retrieved from https://www.scien.cx/2023/02/11/write-promises-chaining-using-async-await/

MLA
" » Write promises chaining using async/await.." Ayanabilothman | Sciencx - Saturday February 11, 2023, https://www.scien.cx/2023/02/11/write-promises-chaining-using-async-await/
HARVARD
Ayanabilothman | Sciencx Saturday February 11, 2023 » Write promises chaining using async/await.., viewed ,<https://www.scien.cx/2023/02/11/write-promises-chaining-using-async-await/>
VANCOUVER
Ayanabilothman | Sciencx - » Write promises chaining using async/await.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/11/write-promises-chaining-using-async-await/
CHICAGO
" » Write promises chaining using async/await.." Ayanabilothman | Sciencx - Accessed . https://www.scien.cx/2023/02/11/write-promises-chaining-using-async-await/
IEEE
" » Write promises chaining using async/await.." Ayanabilothman | Sciencx [Online]. Available: https://www.scien.cx/2023/02/11/write-promises-chaining-using-async-await/. [Accessed: ]
rf:citation
» Write promises chaining using async/await. | Ayanabilothman | Sciencx | https://www.scien.cx/2023/02/11/write-promises-chaining-using-async-await/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.