function fun1() {
return new Promise(resolve => {
setTimeout(() => {
resolve(1)
}, 2000)
})
}
function fun2() {
return new Promise(resolve => {
setTimeout(() => {
resolve(2)
}, 3000)
})
}
// 并行
async function fun6() {
console.time('fun6')
let res = await Promise.all([fun1(),fun2()])
console.log(res) // [1, 2]
console.timeEnd('fun6')
}
fun6() // 3s
// 并行
async function fun8() {
console.time('fun8')
let a = fun1()
let b = fun2()
let c = await a
let d = await b
console.log(c) // 1
console.log(d) // 2
console.timeEnd('fun8')
}
fun8() // 3s
// 串行
async function fun7() {
console.time('fun7')
await fun1()
await fun2()
console.timeEnd('fun7')
}
fun7() // 5s
网友评论