Async/Await
Introduction
async and await make promises easier to write.
asyncmakes a function return a Promise.awaitmakes a function wait for a Promise.
Async Functions
The keyword async before a function makes the function return a
promise.
Await Syntax
The keyword await before a promise makes JavaScript wait
until that promise settles and returns its result.
Note: await only works inside an
async function.
Error Handling
You can handle errors in async functions using standard
try...catch blocks.
Summary
async/awaitis syntactic sugar built on top of Promises.- It makes asynchronous code look and behave a little more like synchronous code.
- Use
try...catchfor error handling.
Quick Quiz
Can you use 'await' outside of an 'async' function?
Enjoying these tutorials?