Created
February 16, 2023 12:10
-
-
Save ghostcode/2753250d68d3e14312026f586779ed49 to your computer and use it in GitHub Desktop.
async&await 并行/串行優化
This file contains 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
function getData() { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve(1) | |
},2000) | |
}) | |
} | |
async function get() { | |
return await getData() | |
} | |
// console.log(Object.prototype.toString.call(get())); | |
(async function init() { | |
const start = console.time() | |
// 1: | |
// const a = await getData() | |
// const b = await getData() | |
// 2: | |
const [a,b] = await Promise.all([getData(),getData()]) | |
// 3. | |
// const aP = getData() | |
// const bP = getData() | |
const end = console.timeEnd(); | |
console.log(start - end); | |
console.log(a+b); | |
// console.log('>>>>>>>',(await aP) + (await bP)); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment