how to use await in node js code example
Example 1: await in node js
function square(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(Math.pow(x, 2));
}, 2000);
});
}
async function layer(x)
{
const value = await square(x);
console.log(value);
}
layer(10);
Example 2: await in node js
function add(a,b){
return a + b;
}
async function add(a,b){
return a + b;
}
Example 3: async await
async function f() {
try {
let response = await fetch('/no-user-here');
let user = await response.json();
} catch(err) {
alert(err);
}
}
f();
Example 4: async await
async function showAvatar() {
await setTimeout(resolve, 3000);
}
showAvatar();