Promise.all([])返回已解决的承诺,但是Promise.race([])返回未决的承诺。他们为什么不同? [英] Promise.all([]) returns a resolved promise, but Promise.race([]) returns a pending promise. Why are they different?

查看:109
本文介绍了Promise.all([])返回已解决的承诺,但是Promise.race([])返回未决的承诺。他们为什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用非空数组调用Promise.all或Promise.race,它们将返回未决的诺言:

If either Promise.all or Promise.race are called with a non-empty array, they return a pending promise:

console.log(Promise.all([1]));
// prints Promise {<pending>}
console.log(Promise.race([1]));
// returns Promise {<pending>}

如果Promise.race被调用一个空数组,它返回一个未完成的promise:

If Promise.race is called with an empty array, it returns a pending promise:

console.log(Promise.race([]));
// prints Promise {<pending>}

但是如果Promise.all被调用带有一个空数组,它返回一个已经解决的承诺:

But if Promise.all is called with an empty array, it returns a promise that is already resolved:

console.log(Promise.all([]));
// prints Promise {<resolved>: Array(0)}

为什么是Promise.all函数是这样设计的吗?似乎没有充分的理由说明不一致的情况,但也许我遗漏了一些东西。

Why was the Promise.all function designed like this? It seems like there's no good reason for the inconsistency, but maybe I'm missing something.

推荐答案

来自 Promise.race()的EMCA脚本规范


如果可迭代参数为空,或者如果可迭代中的所有承诺均未解决,则

If the iterable argument is empty or if none of the promises in iterable ever settle then the pending promise returned by this method will never be settled






Promise.all()规范并不是那么容易遵循,但是基本上,当您将其传递给一个空数组时,它以它们称为 remainingElementsCount 的 0 可以立即解决。


The Promise.all() specification is not quite so easy to follow in this regard, but basically when you pass it an empty array, it starts out with what they refer to as a remainingElementsCount of 0 which lets it resolve immediately.

当您将其传递时,如 Promise.all([1])中的值,则可能将该值包装在 Promise.resolve()中然后跟踪 .then()处理程序,它将在下一个滴答声中解析,因此 console.log(Promise.all([1]))显示它在下一个刻度之前仍处于待处理状态。

When you pass it a value as in Promise.all([1]), it is likely wrapping that value in Promise.resolve() and then tracking the .then() handler from that which will resolve on the next tick so console.log(Promise.all([1])) shows it still pending before the next tick.

从逻辑上讲,这是有道理的。 Promise.race()应该解析为第一个要解决的承诺的值,但是如果您不传递任何内容,那么实际上就没有第一个要解决的值。唯一的其他选择是拒绝或解析为 undefined 或为无效用法引发异常。我不太确定为什么设计师会选择他们所做的结果与其他选择,但至少在规范中有详细说明。

Logically, there's some sense to this. Promise.race() is supposed to resolve to the value of the first promise to resolve, but if you don't pass it anything, then there really is no first resolved value. The only other options would be to reject or resolve to undefined or throw an exception for invalid usage. I'm not quite sure why the designers chose the outcome they did vs. these other choices, but at least it is clearly detailed in the spec.

Promise.all()可以很好地解析为空数组,这是传递空数组的逻辑结果。

Promise.all(), on the other hand, can resolve to an empty array just fine and that's a logical outcome to passing an empty array.


为什么Promise.all函数是这样设计的?

Why was the Promise.all function designed like this?

要真正知道设计者的逻辑,您必须要问其中的一个,或者一直在讨论中,或者在讨论逻辑的地方找到邮件列表讨论。

To really "know" the designers logic, you'd have to ask one of them or have been in the discussion or find mailing list discussions where the logic was discussed.

但是,一个可以一个论证,如果您有可变长度的事物数组,您想等待完成以 Promise.all()完成,则无论数组中是否包含20个项目,该函数都应起作用它或0。对于长度为 0 的数组,它只会在下一个刻度时立即解析,这将既有用又一致,因为没有承诺

But, one can make an argument that if you had a variable length array of things you wanted to wait for to complete with Promise.all() that the function should work whether the array had 20 items in it or 0. In the case for a 0 length array, it would just resolve immediately on the next tick and that would be both useful and consistent as there are no promises to wait for and there's a resolved value (an empty array) that fits and is consistent.

ES6讨论并符合且一致的解析值(空数组)。主题

以下是对从未解决的 Promise.race()的开发讨论的链接: https://github.com/domenic/promises-unwrapping/issues/75。当然,有些人不同意当前的实施。

Here's a link to a development discussion of Promise.race() never resolving: https://github.com/domenic/promises-unwrapping/issues/75. There were certainly people who disagreed with the current implementation.

我个人的观点(在本主题的各种讨论中也得到其他人的赞同)是,它应该抛出异常,因为它基本上是无效条件,从开发的角度来看认为快速失败和引人注目比无限承诺要好得多。但是,显然有更多人喜欢它。

My personal opinion (shared by some others in various discussions of this topic) is that it should have thrown an exception because it's basically an invalid condition and from a development point of view "fail fast and noticeable" is much better than an infinite promise. But, there were apparently more people who liked it the way it is.

蓝鸟文档建议使用其 Promise.any()而不是 Promise.race( ),部分原因是它没有这种行为。

The Bluebird docs recommend using their Promise.any() instead of Promise.race(), partially because it does not have this behavior.

这篇关于Promise.all([])返回已解决的承诺,但是Promise.race([])返回未决的承诺。他们为什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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