What is an async function?
Last updated
Last updated
An async
function is a function that allows you to pause the function's execution while it waits for (await
s) a promise to resolve. It's an abstraction on top of the Promise API that makes asynchronous operations look like they're synchronous.
async
functions automatically return a Promise object. Whatever you return
from the async
function will be the promise's resolution. If instead you throw
from the body of an async
function, that will be how your async function rejects the promise it returns.
Most importantly, async
functions are able to use the await
keyword in their function body, which pauses the function until the operation after the await
completes, and allows it to return that operation's result to a variable synchronously.
async
functions are just syntactic sugar on top of Promises.
They make asynchronous operations look like synchronous operations in your function.
They implicitly return a promise which resolves to whatever your async
function returns, and reject to whatever your async
function throw
s.