受け入れ答えは以下を提供していないので、私は別の答えを追加します。
- 通知をすべての要求が行われたときに
- が順番
に結果を提供
- エラー処理
- 同時に実行するリクエスト数を決定する同時実行制御
この第1の解決策は、事実を大幅に簡略化するため、Bluebird約束ライブラリを使用します。特に、同時実行制御のために:さえずりが一度にすべての要求を送信すると罰金であれば、
const fs = require('fs');
const fetch = require('node-fetch);
// promisify readFile
fs.readFileAsync = function(file, options) {
return new Promise(function(resolve, reject) {
fs.readFile(file, options, function(err, data) {
if (err) return reject(err);
resolve(data);
});
});
}
function getSingleCreationDate(item) {
return fetch('http://mytwitterbirthday.com/api/?screen_name=' + item).then(function(response) {
return response.json();
});
}
function getAllCreationDates(file) {
return fs.readFileAsync(file).then(function(data) {
let array = data.toString().split("\n");
let results = [];
return array.reduce(function(p, item) {
return p.then(function() {
return getSingleCreationDate(item).then(function(twitterData) {
results.push(twitterData);
});
})
}, Promise.resolve()).then(function() {
// make array of results be the resolved value
return results;
});
});
}
getAllCreationDates('file.txt').then(function(results) {
// process array of results here (in order)
}).catch(function(err) {
// handle error here
});
または:
const Promise = require('bluebird');
const fs = Promise.promsifyAll(require('fs'));
const fetch = require('node-fetch);
function getSingleCreationDate(item) {
return fetch('http://mytwitterbirthday.com/api/?screen_name=' + item).then(function(response) {
return response.json();
});
}
function getAllCreationDates(file) {
return fs.readFileAsync(file).then(function(data) {
let array = data.toString().split("\n");
// put whatever concurrency value here works best, higher is more parallelism
// lower is more protection from being rate limited by the host
return Promise.map(array, getSingleCreationDate, {concurrency: 4});
});
}
getAllCreationDates('file.txt').then(function(results) {
// process array of results here (in order)
}).catch(function(err) {
// handle error here
});
この第2の解決策は、標準ES6の約束を使用して、OPの元のコードのような要求をシリアライズ
const fs = require('fs');
const fetch = require('node-fetch);
// promisify readFile
fs.readFileAsync = function(file, options) {
return new Promise(function(resolve, reject) {
fs.readFile(file, options, function(err, data) {
if (err) return reject(err);
resolve(data);
});
});
}
function getSingleCreationDate(item) {
return fetch('http://mytwitterbirthday.com/api/?screen_name=' + item).then(function(response) {
return response.json();
});
}
function getAllCreationDates(file) {
return fs.readFileAsync(file).then(function(data) {
let array = data.toString().split("\n");
return Promise.all(array.map(function(item) {
return getSingleCreationDate(item);
}));
});
}
getAllCreationDates('file.txt').then(function(results) {
// process array of results here (in order)
}).catch(function(err) {
// handle error here
});
注:これにより、すべてのリクエストが一度に(パラレルで - レート制限の問題であれば元のコードはシリアルに実行していましたが)、c結果を順番に取得するか、すべての結果がいつ完了するかを知るためには、odeを追加する必要があります。これは実際にはそれだけでは役に立ちません - ソリューションの一部です。 – jfriend00
これは 'Queue consumer'ではなく、一度に各リクエストを実行します。 – Hitmands
@Hitmands:はい、私はその答えに言いました: "私はあなたが出力と同じ順序で出力を印刷する必要はないと仮定します。 – thameera