我假設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