私はapiへのgetリクエストの配列のための非同期キューを作成しようとしていますが、私はそのレスポンスをどのように組み合わせて使用するか分かりません。おそらく私の実装は間違っています。なぜなら私は約束の中で関数async.queueを使用しているからです。約束内のasync.queue?
最終的に私は最初の約束から結果を取得したいと思い - >
async.queueに対するGETリクエストの配列を作成するために、その最初の約束の使用結果 - >
は、すべての結果を結合応答を得る。私は、APIのレート制限のため、一度に外出するリクエストの量を抑える必要があります。
const rp = require("request-promise");
app.get("/", (req,res) => {
let arr = []
rp.get(url)
.then((response) => {
let arrayID = response
let q = async.queue((task, callback) => {
request({
method: "GET",
url: url,
qs: {
id: task.id
}
}, (error, response, body) => {
arr.push(body)
console.log(arr.length)
// successfully gives me the response i want. im trying to push into an array with all of my responses,
// but when i go to next then chain it is gone or if i try to return arr i get an empty []
})
callback()
}, 3)
for(var i = 0; i < arrayID.length; i++){
q.push({ id : arrayID[i]});
}
q.drain = function() {
console.log('all items have been processed');
}
return arr
})
.then((responseArray) => {
//empty array even though the length inside the queue said other wise, i know its a problem with async and sync actions but is there a way to make the promise chain and async queue play nice?
res.json(responseArray)
})
})
)約束でそれをラップし、q.drainの最後の配列を(解決になってしまった、それを考え出しました私はasync.queueのような何かが必要だったか、それを模倣するPromise.allと微調整することができる何かがありますか?私はpromise.allで本質的に10回することができるようにする必要がありますし、次に別の10をします –