何时使用promise.all()? [英] When to use promise.all()?

查看:179
本文介绍了何时使用promise.all()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更多是一个概念性问题。我了解Promise设计模式,但是找不到可靠的来源来回答有关 promise.all()的问题:

This is more of a conceptual question. I understand the Promise design pattern, but couldn't find a reliable source to answer my question about promise.all():

我唯一想到的是:


  • 如果要兑现承诺,请使用 promise.all()如果所有个promise对象都解决并拒绝,甚至有一个拒绝。

  • Use promise.all() if you want to resolve the promise only if all of the promise objects resolve and reject if even one rejects.

推荐答案

我不确定是否真的有人给出了何时使用 Promise.all()(以及何时不使用它)的最一般用途的解释: / p>

I'm not sure anyone has really given the most general purpose explanation for when to use Promise.all() (and when not to use it):


什么是使用promise.all()的正确方案

What is(are) the correct scenario(s) to use promise.all()

Promise.all()在您有多个诺言并且您的代码想知道何时所有操作都可以使用时很有用这些承诺表示已经成功完成。各个异步操作是什么都没有关系。如果它们是异步的,则由诺言表示,并且您的代码想知道它们何时全部完成,则构建 Promise.all()可以做到这一点。

Promise.all() is useful anytime you have more than one promise and your code wants to know when all the operations that those promises represent have finished successfully. It does not matter what the individual async operations are. If they are async, are represented by promises and your code wants to know when they have all completed, then Promise.all() is built to do exactly that.

例如,假设您需要从三个单独的远程API调用中收集信息,并且在获得所有三个API调用的结果后,则需要使用这三个API来运行一些其他代码结果。对于 Promise.all(),这种情况将是完美的。您可能会这样:

For example, suppose you need to gather information from three separate remote API calls and when you have the results from all three API calls, you then need to run some further code using all three results. That situation would be perfect for Promise.all(). You could so something like this:

Promise.all([apiRequest(...), apiRequest(...), apiRequest(...)]).then(function(results) {
    // API results in the results array here
    // processing can continue using the results of all three API requests
}, function(err) {
    // an error occurred, process the error here
});

Promise.all()可能是最多的通常用于类似类型的请求(如上例中所示),但没有必要这样做。如果在不同情况下需要发出远程API请求,则读取本地文件并读取本地温度探测器,然后当您拥有所有三个异步操作的数据时,您希望对来自所有异步数据的数据进行一些处理。第三,您将再次使用 Promise.all()

Promise.all() is probably most commonly used with similar types of requests (as in the above example), but there is no reason that it needs to be. If you had a different case where you needed to make a remote API request, read a local file and read a local temperature probe and then when you had data from all three async operations, you wanted to then do some processing with the data from all three, you would again use Promise.all():

Promise.all([apiRequest(...), fs.readFileAsync(...), readTemperature(...)]).then(function(results) {
    // all results in the results array here
    // processing can continue using the results of all three async operations
}, function(err) {
    // an error occurred, process the error here
});

另一方面,如果您不需要在它们之间进行协调,只需处理每个异步单独进行操作,则不需要 Promise.all()。您可以使用自己的 .then()处理程序来触发每个单独的异步操作,而无需它们之间的协调。

On the flip side, if you don't need to coordinate among them and can just handle each async operation individually, then you don't need Promise.all(). You can just fire each of your separate async operations with their own .then() handlers and no coordination between them is needed.

此外, Promise.all()具有所谓的快速失败实现。它会返回一个主承诺,该主承诺会在您通过的第一个承诺被拒绝时立即拒绝,或者在所有承诺都解决后才会解决。因此,要使用 Promise.all(),这种实现类型需要适合您的情况。在其他情况下,您希望运行多个异步操作,并且需要所有结果,即使其中一些失败。 Promise.all()不会直接为您完成此操作。相反,您可能会在这种情况下使用 Promise.settle()之类的东西。您可以看到 的实现.settle()此处,即使某些操作失败,您也可以访问所有结果。当您期望某些操作可能会失败并且您要执行一项有用的任务以获取成功操作的结果,或者想要检查所有未能基于此做出决策的操作的失败原因时,这特别有用。

In addition Promise.all() has what is called a "fast fail" implementation. It returns a master promise that will reject as soon as the first promise you passed it rejects or it will resolve when all the promises have resolved. So, to use Promise.all() that type of implementation needs to work for your situation. There are other situations where you want to run multiple async operations and you need all the results, even if some of them failed. Promise.all() will not do that for you directly. Instead, you would likely use something like Promise.settle() for that situation. You can see an implementation of .settle() here which gives you access to all the results, even if some failed. This is particularly useful when you expect that some operations might fail and you have a useful task to pursue with the results from whatever operations succeeded or you want to examine the failure reasons for all the operations that failed to make decisions based on that.


是否有最佳实践来使用promise.all()?仅当所有promise对象具有相同或相似的类型时,才应该使用
吗?

Are there any best practices to use promise.all()? Should it be ideally used only if all of the promise objects are of the same or similar types?

如上所述,无关的异步操作是什么,或者它们是否是同一类型都无所谓。关键是您的代码是否需要协调它们并知道它们何时全部成功。

As explained above, it does not matter what the individual async operations are or if they are the same type. It only matters whether your code needs to coordinate them and know when they all succeed.

列出某些情况下使用 Promise.all():


  1. 只有一个异步操作时。仅需执行一项操作,就可以对一个诺言使用 .then()处理程序,并且没有理由使用 Promise.all()

  2. 当您不需要在多个异步操作之间进行协调时。

  3. 当快速失败实现不合适时。如果您需要所有结果,即使某些结果失败,则 Promise.all()本身不会这样做。您可能会希望使用类似 Promise.settle()的东西。

  4. 如果异步操作未全部返回诺言,则 Promise.all()无法跟踪未通过承诺管理的异步操作。

  1. When you only have one async operation. With only one operation, you can just use a .then() handler on the one promise and there is no reason for Promise.all().
  2. When you don't need to coordinate among multiple async operations.
  3. When a fast fail implementation is not appropriate. If you need all results, even if some fail, then Promise.all() will not do that by itself. You will probably want something like Promise.settle() instead.
  4. If your async operations do not all return promises, Promise.all() cannot track an async operation that is not managed through a promise.

这篇关于何时使用promise.all()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆