Skip to content

Instantly share code, notes, and snippets.

@ghostcode
Created February 16, 2023 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghostcode/2753250d68d3e14312026f586779ed49 to your computer and use it in GitHub Desktop.
Save ghostcode/2753250d68d3e14312026f586779ed49 to your computer and use it in GitHub Desktop.
async&await 并行/串行優化
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