Skip to content

Instantly share code, notes, and snippets.

@srsajjad
Created March 10, 2018 06:02
Show Gist options
  • Save srsajjad/3c5de46e6e4e241d2985f264ca79038d to your computer and use it in GitHub Desktop.
Save srsajjad/3c5de46e6e4e241d2985f264ca79038d to your computer and use it in GitHub Desktop.
// this code pauses and executes one after another
function five() {
return new Promise((resolve, reject) => {
setTimeout(() => resolve("i am after five seconds"), 5000);
});
}
function three() {
return new Promise((resolve, reject) => {
setTimeout(() => resolve("i am after three seconds"), 3000);
});
}
async function test() {
let fiveData = await five();
let threeData = await three();
console.log(fiveData);
console.log(threeData);
}
test();
// this code executes in parallel
function five() {
return new Promise((resolve, reject) => {
setTimeout(() => resolve("i am after 5 seconds"), 5000);
});
}
function three() {
return new Promise((resolve, reject) => {
setTimeout(() => resolve("i am after 3 seconds"), 3000);
});
}
function one() {
return new Promise((resolve, reject) => {
setTimeout(() => resolve("i am after 1 second"), 1000);
});
}
async function test() {
[fiveData, threeData, oneData] = await Promise.all([five(), three(), one()]);
console.log(fiveData);
console.log(threeData);
console.log(oneData);
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment