async in promise code example
Example 1: async awiat
const data = async () => {
const got = await fetch('https://jsonplaceholder.typicode.com/todos/1');
console.log(await got.json())
}
data();
Example 2: nodejs promise async
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 3: nodejs promise async
function add(a,b){
return a + b;
}
async function add(a,b){
return a + b;
}
Example 4: async await
async function showAvatar() {
await setTimeout(resolve, 3000);
}
showAvatar();