What is the await node in async?

The await and aync operators are used in Javascript to allow asynchronous, Promise-based behavior. The await operator can only be used in an async function within regular JavaScript code; however, it can be used on its own with JavaScript modules.

Promise-based asynchronous programs simply await the function that returns the Promise. However, the function async needs to be declared before awaiting a function returning a Promise.

The await expression causes the async function to wait for the resolution of Promise before resuming the execution of the program. This promise can either be resolved or rejected, and then the value is returned to the await expression.

Code

The following code explains how the await expression receives the value of the Promise.

async function f() {
let p = new Promise((resolve, reject) => {
setTimeout(() => resolve("done!"), 1000)
});
let result = await p;
// the program halts execution at line 6 until
// the promise p resolves or returns
// an error
console.log(result); // This is printed only after the promise p
// returns a value(resolved or rejected)
}
f();

Free Resources