function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
async function add1(x) {
const a = await resolveAfter2Seconds(20);
const b = await resolveAfter2Seconds(30);
return x + a + b;
}
add1(10).then(v => {
console.log(v); // prints 60 after 4 seconds.
});
async function add2(x) {
const p_a = resolveAfter2Seconds(20);
const p_b = resolveAfter2Seconds(30);
return x + await p_a + await p_b;
}
add2(10).then(v => {
console.log(v); // prints 60 after 2 seconds.
});
const API_LIST = [url1, url2, url3, ...];
async function someFunction(urls) {
for (const url of urls) {
const res = await fetch(url);
// do something
}
}
async function someFunction(urls) {
const somePromises = urls.map(url => {
return fetch(url);
});
for (const apiPromise of somePromises) {
const res = await apiPromise;
//do something with res
}
}