我想創建一個從服務器獲取數據的請求鏈,但是在每個請求之間應該發生延遲X秒。爲延遲請求處理創建一個承諾鏈
應該是這樣的:
const data = {};
const promises = Promise.resolve();
for (let elem of longArray) {
promises.then(() => {
return sendRequest(); // returns promise
})
.then((response) => {
// Store response stuff in data
})
.then(() => {
// Wait here for X seconds before continuing
})
}
promises.finally(() => {
// Log stuff from data
});
不過,我不明白這一點做我想要的。它立即啓動所有請求,然後進入響應處理程序。數據填充之前調用finally部分。
的我不知道你想要的行爲但是,你有沒有試過[Promise.all](http://bluebirdjs.com/docs/api/promise.all.html)? –
是的,我有。使用Promise.all立即運行我在數組中收集的所有承諾 - 無需延遲。 – akohout