Jobs
Blogs
Courses
Mentorship
Partners
For Employers
e2e logo

Async and Await

mac

Async function.

Async is a keyword to create async function. An async function declaration creates an Async function object. Each time when an async function is called, it returns a new promise which will be resolved with the value returned by the async function, or rejected with an exception uncaught within the async function.

Async functions can contain zero or more await expressions. Await expressions make promise-returning functions behave as though they're synchronous by suspending execution until the returned promise is fulfilled or rejected. The resolved value of the promise is treated as the return value of the await expression. Use of async and await enables the use of ordinary try / catch blocks around asynchronous code.

Asynchronous programming is a better fit for code that must respond to events – for example, any kind of graphical UI. An example of a situation where programmers use async but shouldn't is any code that can focus entirely on data processing and can accept a “stop-the-world” block while waiting for data to download.


Note: It always return promise.


If we not write the a promise inside a function, async itself returns a value with wrapping of returns a promise.


async function getdata() {

return "Namaste";

}

const data = getdata();

Console.log(data);


Output:

promise { < fulfilled >: Namaste}


If we have to get data out from promise, have to use ".then" method.

const data promise = getdata ();

data promise. then ( res => console.log(res));


It gives normal gives Namaste in output


Async with Await ( It is used to handle promises)

Await is a keyword that can only used inside the async function.

Await is used to unwrap promises by passing a promise as the expression. Using await process the execution of its surroundings async function until the promise is settled (i.e: fulfilled/rejected) When execution resumes, the value of the await expression becomes that of fulfilled promise.


Syntax/Example:

const p= new promise((resolve, reject)=>{

resolve("Promise Resolved Value!!");

});

async function handlepromise(){

const value= await p;

console.log(value);

}

handlepromise();


JS engine will not wait for promise to be resolved, other will get executes.

const p= new promise((resolve, reject)=>{

set TimeOut(()=>{

resolve("Promise Resolved Value!!");

}); 10000);

async function handlepromise(){

const value= await p;

console.log("Namaste Javascript");

console.log(value);

}

handlepromise();


Output:

(after 10seconds)

Namste Javascript

Promise resolved value!!


If we give console.log("Hello") before await, it does not waits. Quick result will come.


Async and Await provide a powerful and elegant way to handle asynchronous operations in JavaScript. By allowing you to write asynchronous code that looks synchronous, they can make your code more readable and easier to manage. Just remember to handle errors appropriately and to understand that async functions always return Promises.