Async functions

An async function is a function declared with the async keyword. Async functions are instances of the AsyncFunction constructor, and the await keyword is permitted within them. The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains.

Example:

function resolveAfter2Seconds() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, 2000);
  });
}

async function asyncCall() {
  console.log('calling');
  const result = await resolveAfter2Seconds();
  console.log(result);
  // expected output: "resolved"
}

asyncCall();

Async functions may also be defined as expressions.

async function name([param[, param[, ...param]]]) {
   statements
}

Parameters:
name – The function’s name.
param – The name of an argument to be passed to the function.
statements – The statements comprising the body of the function. The await mechanism may be used.
Return value – A Promise which will be resolved with the value returned by the async function, or rejected with an exception thrown from, or uncaught within, the async function.

Leave a comment

Your email address will not be published. Required fields are marked *