あなたが同時に飛行中のあるどのように多くの要求を制御するための同時実行オプションがあるBluebird's .map()
使用することができます。FYI
const Promise = require('bluebird');
const http = Promise.promisifyAll(require('http');
var ids = {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 6: 56, 7: 7, 8: 8, 5:6 }; // this is random
Promise.map(Object.keys(ids).map(function(dp){
return http.post({url: addr, form: { data: dp }).then(function(body) {
return body.xx;
});
}), {concurrency: 2}).then(function(results) {
// process results here
});
を、私はあなたの第二で何をしようとしていたものを理解していませんでしたhttp.post()
data.x
を参照している場合、data
が配列のためです。私はそのコードが実際に何をしようとしていたかを説明するためにあまりにも多くの疑似コードであると思う。http.post()
そうしないと、あなたが行うにはより多くのを持っていなくなるまで、あなたが別のを起動、たびに、最初は、その後、1つの仕上げを使用すると、Nリクエストを起動し、独自の同時実行制御をコーディングすることができます。ここでは、手動で同時実行制御を符号化する例を示します
Fire off 1,000,000 requests 100 at a time
それとも、あなたはこのようにそれを自分で書くことができます:
const http = require('http');
function httpPost(options) {
return new Promise(function(resolve, reject) {
http.post(options, function(err, res, body) {
if (err) {
reject(err);
} else {
resolve(body);
}
});
});
}
// takes an array of items and a function that returns a promise
function mapConcurrent(items, maxConcurrent, fn) {
let index = 0;
let inFlightCntr = 0;
let doneCntr = 0;
let results = new Array(items.length);
let stop = false;
return new Promise(function(resolve, reject) {
function runNext() {
let i = index;
++inFlightCntr;
fn(items[index], index++).then(function(val) {
++doneCntr;
--inFlightCntr;
results[i] = val;
run();
}, function(err) {
// set flag so we don't launch any more requests
stop = true;
reject(err);
});
}
function run() {
// launch as many as we're allowed to
while (!stop && inflightCntr < maxConcurrent && index < items.length) {
runNext();
}
// if all are done, then resolve parent promise with results
if (doneCntr === items.length) {
resolve(results);
}
}
run();
});
}
var ids = {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 6: 56, 7: 7, 8: 8, 5:6 }; // this is random
mapConcurrent(Object.keys(ids), 2, function(item, index) {
return httpPost({url: addr, form: {data: item}}).then(function(body) {
return body.xxx;
});
}).then(function(results) {
// array of results here
}, function(err) {
// error here
});
https://github.com/ForbesLindesay/throatあなたはhttp://stackoverflow.com/a/を見てくださいどのように多くの同時約束は –
を実行することができます制限できます38778887/1048572またはhttp://stackoverflow.com/a/39197252/1048572。 – Bergi