Bluebird 异步系列调用
Bluebird async series call
我有下面的代码,但是一旦将 widgetsAddCall 添加到数组中,它就会被执行,promise.each 没有用。
函数 widgetsAddCall 正在向 API 服务器和 returns bluebird promisified 请求发出异步请求。我希望 API 调用一个接一个地进行,以便每个调用一个接一个地发送数据块。
var chunkCnt = Math.ceil(widgetsIds.length/4000);
var responseT = Array();
var Promise = require('bluebird');
for(var cntTemp =0 ; cntTemp<chunkCnt;cntTemp++){
var tempWidgs = widgetsIds.slice(cntTemp,cntTemp+4000);
var query = {
id: tempWidgs.join(',')
};
responseT.push(widgetsAddCall(tempWidgs,query,campRemoteId,campaign));
}
return Promise.each(responseT,function(responses) {
// Use the responses here
return getWidgets(campRemoteId,campaign).then((ids) => {
var toRemove = [];
for(var id of ids){
if(widgetsIds.indexOf(id)===-1){
toRemove.push(id);
}
}
if(toRemove.length) {
return removeWidgets(campaign, campRemoteId, toRemove);
}
});
})
I want that API call is made one after another, so that each call
sends chunk of data one after another.
我能想到的序列化所有调用的最简单方法是将块预先构建到数组中,然后使用 Bluebird 的 Promise.mapSeries()
连续遍历数组:
var Promise = require('bluebird');
// pre-build chunks into an array
var chunks = [];
var chunkCnt = Math.ceil(widgetsIds.length / 4000);
for (var chunkIndex = 0; chunkIndex < chunkCnt; chunkIndex++) {
chunks.push(widgetsIds.slice(cntIndex,cntIndex + 4000));
}
// now serially iterate the array
Promise.mapSeries(chunks, function(item) {
return widgetsAddCall(item, {id: item.join(',')}, campRemoteId, campaign);
}).then(function(results) {
// now process the results of widgetsAddCall() here
});
仅供参考,您最初使用 Promise.each()
对我来说没有任何意义,因为您正在迭代一系列承诺,但随后没有使用迭代中的任何信息。迭代似乎没有意义。另外,您没有正确序列化对 widgetsAddCall()
的调用,因为您是并行启动所有这些调用的。
我有下面的代码,但是一旦将 widgetsAddCall 添加到数组中,它就会被执行,promise.each 没有用。
函数 widgetsAddCall 正在向 API 服务器和 returns bluebird promisified 请求发出异步请求。我希望 API 调用一个接一个地进行,以便每个调用一个接一个地发送数据块。
var chunkCnt = Math.ceil(widgetsIds.length/4000);
var responseT = Array();
var Promise = require('bluebird');
for(var cntTemp =0 ; cntTemp<chunkCnt;cntTemp++){
var tempWidgs = widgetsIds.slice(cntTemp,cntTemp+4000);
var query = {
id: tempWidgs.join(',')
};
responseT.push(widgetsAddCall(tempWidgs,query,campRemoteId,campaign));
}
return Promise.each(responseT,function(responses) {
// Use the responses here
return getWidgets(campRemoteId,campaign).then((ids) => {
var toRemove = [];
for(var id of ids){
if(widgetsIds.indexOf(id)===-1){
toRemove.push(id);
}
}
if(toRemove.length) {
return removeWidgets(campaign, campRemoteId, toRemove);
}
});
})
I want that API call is made one after another, so that each call sends chunk of data one after another.
我能想到的序列化所有调用的最简单方法是将块预先构建到数组中,然后使用 Bluebird 的 Promise.mapSeries()
连续遍历数组:
var Promise = require('bluebird');
// pre-build chunks into an array
var chunks = [];
var chunkCnt = Math.ceil(widgetsIds.length / 4000);
for (var chunkIndex = 0; chunkIndex < chunkCnt; chunkIndex++) {
chunks.push(widgetsIds.slice(cntIndex,cntIndex + 4000));
}
// now serially iterate the array
Promise.mapSeries(chunks, function(item) {
return widgetsAddCall(item, {id: item.join(',')}, campRemoteId, campaign);
}).then(function(results) {
// now process the results of widgetsAddCall() here
});
仅供参考,您最初使用 Promise.each()
对我来说没有任何意义,因为您正在迭代一系列承诺,但随后没有使用迭代中的任何信息。迭代似乎没有意义。另外,您没有正确序列化对 widgetsAddCall()
的调用,因为您是并行启动所有这些调用的。