我假设Q.all([p1, p2, p3]).then(f)
与
p1.then(function () {
p2.then(function () {
p3.then(f);
});
});
因为当创建 p1
、p2
和 p3
时,异步调用已经进行,我们只需要等待所有这些调用来解决,顺序并不重要。
我说得对吗?
如果是这样,我一直在查看 kriskowal's all
implementation 。我认为这会是类似的东西(使用 then
链接 promise )。但我发现它的实现方式完全不同?这是为什么?
编辑:
让我说得更清楚一些。假设 p1、p2、p3 分别在 100ms、200ms、300ms 内解析。等待响应的顺序没有区别
p1.then(function () {
// we're here at 100ms
p2.then(function () {
// 100ms later we're here
p3.then(f); // f gets called 100ms later (at 300ms)
});
});
p3.then(function () {
// we're here at 300ms
p2.then(function () {
// boom! resolves in a snap
p1.then(f); // f also gets called instantly (at 300ms)
});
});
在这两个示例中,我们只等待 300 毫秒即可解决所有三个 Promise。
最佳答案
I'm assuming
Q.all([p1, p2, p3]).then(f)
is no different thanp1.then(function () { p2.then(function () { p3.then(f); }); });
不完全是。 f
正在传递一个结果数组,并且您总是希望返回
( promise )结果;所以你会写
p1.then(function(r1) {
return p2.then(function(r1) {
return p3.then(function(r3) {
return f([r1, r2, r3]);
});
});
});
// or equivalently, better matching a separate `Q.all`:
p1.then(function(r1) {
return p2.then(function(r1) {
return p3.then(function(r3) {
return [r1, r2, r3];
});
});
}).then(f);
Because when p1, p2 and p3 are created the async calls have already been made and we just have to wait for all of them to resolve and the order doesn't matter.
Am I correct?
有一点。事实上,.all
的简单版本可以像这样实现。
但是,我们需要考虑一个重要的“边缘”情况:当 promise 没有得到履行而是被拒绝时。突然,回调的嵌套结构变得错误 - 因为我们想要从头开始并行地观察所有三个 Promise。假设 p3
在 200 毫秒后解析,p1
在 300 毫秒后解析,p2
在 100 毫秒后被拒绝。
使用嵌套的 then
回调,在第一次观察到 p2
之前,它将等待整个 300 毫秒 (p1
) - 但是,它早就被拒绝了。相反,.all
方法希望尽早失败,并在任何传递的 Promise 被拒绝后立即拒绝结果 Promise。 p>
因此(还有一点是为了性能),Q.all
在内部使用 deferred pattern .
关于javascript - 为什么Q.all要这样实现呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26816345/