await Promise.all() 和 multiple await 有什么区别?

编程入门 行业动态 更新时间:2024-10-27 17:17:35
本文介绍了await Promise.all() 和 multiple await 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有什么区别:

const [result1, result2] = await Promise.all([task1(), task2()]);

const t1 = task1(); const t2 = task2(); const result1 = await t1; const result2 = await t2;

const [t1, t2] = [task1(), task2()]; const [result1, result2] = [await t1, await t2];

推荐答案

注意:

这个答案只涵盖了 await 系列和 Promise.all 之间的时间差异.请务必阅读@mikep 的综合答案,其中还涵盖了错误处理中更重要的差异.

This answer just covers the timing differences between await in series and Promise.all. Be sure to read @mikep's comprehensive answer that also covers the more important differences in error handling.

为了回答这个问题,我将使用一些示例方法:


For the purposes of this answer I will be using some example methods:

  • res(ms) 是一个函数,它接受一个整数毫秒并返回一个在该毫秒后解析的承诺.
  • rej(ms) 是一个函数,它接受一个整数毫秒并返回一个承诺,该承诺在该毫秒后拒绝.
  • res(ms) is a function that takes an integer of milliseconds and returns a promise that resolves after that many milliseconds.
  • rej(ms) is a function that takes an integer of milliseconds and returns a promise that rejects after that many milliseconds.

调用 res 启动计时器.使用 Promise.all 等待一些延迟会在所有延迟完成后解决,但请记住它们是同时执行的:

Calling res starts the timer. Using Promise.all to wait for a handful of delays will resolve after all the delays have finished, but remember they execute at the same time:

const data = await Promise.all([res(3000), res(2000), res(1000)]) // ^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^ // delay 1 delay 2 delay 3 // // ms ------1---------2---------3 // =============================O delay 1 // ===================O delay 2 // =========O delay 3 // // =============================O Promise.all

async function example() { const start = Date.now() let i = 0 function res(n) { const id = ++i return new Promise((resolve, reject) => { setTimeout(() => { resolve() console.log(`res #${id} called after ${n} milliseconds`, Date.now() - start) }, n) }) } const data = await Promise.all([res(3000), res(2000), res(1000)]) console.log(`Promise.all finished`, Date.now() - start) } example()

这意味着 Promise.all 将在 3 秒后解析来自内部 Promise 的数据.

This means that Promise.all will resolve with the data from the inner promises after 3 seconds.

但是,Promise.all 有一个快速失败"行为:

But, Promise.all has a "fail fast" behavior:

const data = await Promise.all([res(3000), res(2000), rej(1000)]) // ^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^ // delay 1 delay 2 delay 3 // // ms ------1---------2---------3 // =============================O delay 1 // ===================O delay 2 // =========X delay 3 // // =========X Promise.all

async function example() { const start = Date.now() let i = 0 function res(n) { const id = ++i return new Promise((resolve, reject) => { setTimeout(() => { resolve() console.log(`res #${id} called after ${n} milliseconds`, Date.now() - start) }, n) }) } function rej(n) { const id = ++i return new Promise((resolve, reject) => { setTimeout(() => { reject() console.log(`rej #${id} called after ${n} milliseconds`, Date.now() - start) }, n) }) } try { const data = await Promise.all([res(3000), res(2000), rej(1000)]) } catch (error) { console.log(`Promise.all finished`, Date.now() - start) } } example()

如果你改用 async-await,你将不得不等待每个 promise 依次解析,这可能效率不高:

If you use async-await instead, you will have to wait for each promise to resolve sequentially, which may not be as efficient:

const delay1 = res(3000) const delay2 = res(2000) const delay3 = rej(1000) const data1 = await delay1 const data2 = await delay2 const data3 = await delay3 // ms ------1---------2---------3 // =============================O delay 1 // ===================O delay 2 // =========X delay 3 // // =============================X await

async function example() { const start = Date.now() let i = 0 function res(n) { const id = ++i return new Promise((resolve, reject) => { setTimeout(() => { resolve() console.log(`res #${id} called after ${n} milliseconds`, Date.now() - start) }, n) }) } function rej(n) { const id = ++i return new Promise((resolve, reject) => { setTimeout(() => { reject() console.log(`rej #${id} called after ${n} milliseconds`, Date.now() - start) }, n) }) } try { const delay1 = res(3000) const delay2 = res(2000) const delay3 = rej(1000) const data1 = await delay1 const data2 = await delay2 const data3 = await delay3 } catch (error) { console.log(`await finished`, Date.now() - start) } } example()

更多推荐

await Promise.all() 和 multiple await 有什么区别?

本文发布于:2023-11-23 19:26:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   await   Promise   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!