Async/Await
With promises, one can use a async
keyword to declare an asynchronous function that returns a promise whereas the await
syntax makes JavaScript wait until that promise settles and returns its value. These keywords make promises easier to write. An example of async is shown below.
The above example can be written as follows:
async
ensures that the function returns a promise, and wraps non-promises in it. With await
, we can make JavaScript wait until the promise is settled with its value returned.
The await
keyword can only be used inside an async
function.
Last updated
Was this helpful?