Day7
Async/Await
Youtube resources: Async/Await
Resouces: [Async/Await]
Async/Await
- A way to write asynchronous cide in JavaSCrip
- It is non blocking
- Async/Await was created to simplify the process of working with and writing chained promises
- Async/Await is the extension of promises
Async
Async functions return a Promise. If the function throws an error, the Promise will be rejected. If the function returns a value, the Promise will be resolved.
Syntax
Writing async function is quite simple. You just need to add the async keyword prior to function
// Normal Function
function add(x, y) {
return x + y;
}
// Async Function
async function add(x, y) {
return x + y;
}
Await
Async functions can make use of the await expression. This will pause the async function and wait for the Promise to resolve prior to moving on.
Example
const getData = async () => {
var y = await "Hello";
console.log(y);
};
console.log(1);
getData();
console.log(2);
Output:
1
2
Hello
// Note: Console prints 2 before "Hello".