Created
March 10, 2018 06:02
-
-
Save srsajjad/3c5de46e6e4e241d2985f264ca79038d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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