FrontendDeveloper.in

ECMAScript question detail

Async functions

In ES6, Promises were introduced to solve the famous callback hell problem. When a series of nested asynchronous functions need to be executed in order, it leads to a callback hell

function task() {
task1((response1) => {
task2(response1, (response2) => {
task3(response2, (response3) => {
// etc...
};
});
});
}

But the Chained Promises creates complex flow for asynchronous code.

Async functions were introduced as a combination of promises and generators to give us the possibility of writing asynchronous in a synchronous manner. i.e, This function is going to be declared with the async keyword which enable asynchronous, promise-based behavior to be written in a cleaner style by avoiding promise chains. These functions can contain zero or more await expressions.

Let's take a below async function example,

async function logger() {
let data = await fetch('http://someapi.com/users'); // pause until fetch returns
console.log(data)
}
logger();
Back to all ECMAScript questions
Get LinkedIn Premium at Rs 399