async and await
async function test() {
//Invoking an async function returns a promise.
const promise1 = test2();
console.log(promise1);
const output = await promise1.catch((error) => console.log(error));
console.log("Output = " + output);
const promise2 = test3();
console.log(promise2);
const output2 = await promise2.catch((error) => console.log(error));
console.log("Output = " + output2);
//The following commented code is incorrect as the function test3 doesn't return a promise and also not an async
//function.
//The await operator is used to wait for a Promise. It can only be used inside an async function.
//If the value is not a Promise, it converts the value to a resolved Promise, and waits for it.
//const output3 = await test4().catch((error) => console.log(error));
const output3 = await test4();
console.log("Output = " + output3);
const promise3 = test5();
console.log(promise3);
const output4 = await promise3;
console.log("Output = " + output4);
}
async function test2() {
return new Promise((resolve, reject) => {
setTimeout(function(){
resolve(100);
}, 5000);
});
}
async function test3() {
// throw Error("Testing Errors.");
return 200;
}
function test4() {
return 300;
}
function test5() {
return new Promise((resolve, reject) => {
setTimeout(function(){
resolve(400);
}, 5000);
});
}
test();
--------------------------------------------------------------
Output:
Promise { <pending> }
//after 5 seconds
Output = 100
Promise { 200 }
Output = 200
Output = 300
Promise { <pending> }
// after 5 seconds
Output = 400
//Invoking an async function returns a promise.
const promise1 = test2();
console.log(promise1);
const output = await promise1.catch((error) => console.log(error));
console.log("Output = " + output);
const promise2 = test3();
console.log(promise2);
const output2 = await promise2.catch((error) => console.log(error));
console.log("Output = " + output2);
//The following commented code is incorrect as the function test3 doesn't return a promise and also not an async
//function.
//The await operator is used to wait for a Promise. It can only be used inside an async function.
//If the value is not a Promise, it converts the value to a resolved Promise, and waits for it.
//const output3 = await test4().catch((error) => console.log(error));
const output3 = await test4();
console.log("Output = " + output3);
const promise3 = test5();
console.log(promise3);
const output4 = await promise3;
console.log("Output = " + output4);
}
async function test2() {
return new Promise((resolve, reject) => {
setTimeout(function(){
resolve(100);
}, 5000);
});
}
async function test3() {
// throw Error("Testing Errors.");
return 200;
}
function test4() {
return 300;
}
function test5() {
return new Promise((resolve, reject) => {
setTimeout(function(){
resolve(400);
}, 5000);
});
}
test();
--------------------------------------------------------------
Output:
Promise { <pending> }
//after 5 seconds
Output = 100
Promise { 200 }
Output = 200
Output = 300
Promise { <pending> }
// after 5 seconds
Output = 400
Comments
Post a Comment