我假设Q.all([p1, p2, p3]).then(f)
是没有什么不同为什么Q.all像这样执行?
p1.then(function() {
p2.then(function() {
p3.then(f);
});
});
因为当p1
,p2
和p3
创建的异步调用已经作出,我们只需要等待他们都解决和没有按顺序没关系。
我正确吗?
如果是这样,我一直在寻找kriskowal's all
implementation。我认为这会是类似的事情(链接承诺使用then
)。但我发现它的实施方式完全不同?这是为什么?
编辑:
让我更加清楚一点。说p1,p2,p3分别在100ms,200ms,300ms解析。等待他们的反应顺序不作任何differens
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)
});
});
在这两个例子中,我们只能等待所有三项承诺300ms的解决。
谢谢你的回答,但我认为你错了。我认为,当我们有p1和p2以及p3时,意味着所有三个请求都已发送,我们只是在等待他们的回应。我们“等待”的顺序并不重要。这不像我们发送p1,等待响应,然后发送p2。 – adm 2014-11-08 11:44:50
@Arthur:请注意'p1','p2','p3'是承诺,而不是创建承诺的调用。 – Bergi 2014-11-08 13:25:22
真的!你的解释很棒:) – 2014-11-08 19:10:14