以前、p-limit
というライブラリを使ってPromiseを返す関数の同時実行を制御したことがある
これとは別のやり方でes6-promise-pool
を使った方法をしったので調査してみる
参考にしたstackoverflow stackoverflow.com
READMEのusage
// On the Web, leave out this line and use the script tag above instead. var PromisePool = require('es6-promise-pool') var promiseProducer = function () { // Your code goes here. // If there is work left to be done, return the next work item as a promise. // Otherwise, return null to indicate that all promises have been created. // Scroll down for an example. } // The number of promises to process simultaneously. var concurrency = 3 // Create a pool. var pool = new PromisePool(promiseProducer, concurrency) // Start the pool. var poolPromise = pool.start() // Wait for the pool to settle. poolPromise.then(function () { console.log('All promises fulfilled') }, function (error) { console.log('Some promise rejected: ' + error.message) })
特段どちらがよいとかはないみたいだが、p-limit
の方が若干シンプルかなと思う