私はnode.jsの一括ダウンローダを作成しており、ブルーバードの約束を理解しようとしています。 並列要求とディスク書き込みの回数を制限します。 私が理解しているように、Promise.map()
を{concurrent: }
としたいと思います。Node.jsの一括ダウンロードとBluebirdの約束
pipe()
とhttp.get()
は自動的にお約束できないため、私はカスタム約束を使用しようとしています。
しかし、私はthen()
メカニズムを完全に理解していません。 私には、返品された約束は、チェーン全体が履行されたときにのみ満たされるように思えます。
しかし、私のコードでは、チェーン内の最初の約束事はmap()
によって待たれているように見え、多くの要求とディスクの書き込みは並行して行われます。
import Promise from 'bluebird';
import fs from 'fs';
import https from 'https';
Promise.promisifyAll(fs);
Promise.map(Images, image => {
console.log("Opening image " + image.id);
let file = fs.createWriteStream(dir + '/' + image.id + '.jpg');
return new Promise((resolve, reject) => {
console.log("Downloading image " + image.id);
https.get(image.url, resolve).on("error", reject);
})
.then(response => {
response.pipe(file);
console.log("Saving image " + image.id);
return new Promise((resolve, reject) => {
file.on("finish", resolve);
file.on("error", reject);
});
})
.then(() => {
console.log("Finished writing image " + image.id);
file.close();
})
.catch(e => {
console.log("Error during image save of " + image.id + ": " + e.code)
});
}, {concurrent: 50})
.then(res => {
console.log("Finished writing all images")
})
.catch(e => {
console.log("Some images failed to be written: " + e.code)
});
}
私は間違っていますか?約束の履行と拒絶の流れを理解するのを助けてくれますか?
を考え出すことができる最短の作業例です。あなたは、50以上のダウンロード+ディスクライタが同時に発生していると言っていますか?あなたは結果のログを投稿できますか(縮尺の例では、並行処理5の10個のファイル)、お願いしますか? – Bergi
私はそれを理解しました。キーワードは 'concurrent'ではなく' concurrency'です。失敗 –