私は文書の空の配列を持っています。Bluebird約束の数を変えるには? (Nodejs)
let arrayOfDocuments = [];
私は、テキストファイルをダウンロードして、私のarrayOfDocumentsにその内容を入れて(たSuperAgentを使用して)HTTPリクエストを呼び出したいです。
request.get('docs.google.com/document/d/SOME_FILE_NAME').then((res) => {
arrayOfDocuments.push(res.text);
});
私はそれを得るが、ここでは難しい部分です。私はこれをforループに入れて、forループの後に何かしたい。したがって、次のようになります。
for (let i = 0; i < numOfLinks; i++) {
// send the http requests as above
}
//do stuff here but only after the above for loop is finished.
ループが終了したら最後の行だけを実行しますか?私のプログラムが現在実行されている方法では、http要求が応答して終了する前にforループの後のコードが実行されます。私はBluebird Promisesを使ってこれを行う方法があると思いますが、わかりません。ありがとう!あなたがPromise.map
メソッドを使用することができます
var promises = []
var links = ['a.com/a', 'a.com/b']
for (let i = 0; i < links.length; i++) {
promises.push(request.get(links[i])
}
Promise.all(promises).then(function(allRes) {
//do anything you want with allRes or iterate
for (var promise in promises){
promise.then(function(singleRes){/*do something with each promise after all resolve*/}
}
});
を[ 'Promise.all'](http://bluebirdjs.com/docs/api/promise.all.html)。 –
どのようにリクエストを送信したのですか? – user3835653