UGA Boxxx

つぶやきの延長のつもりで、知ったこと思ったこと書いてます

【JavaScript】es6-promise-poolでPromiseを返す関数の同時実行を制御する

以前、p-limitというライブラリを使ってPromiseを返す関数の同時実行を制御したことがある

uga-box.hatenablog.com

これとは別のやり方でes6-promise-poolを使った方法をしったので調査してみる

参考にしたstackoverflow stackoverflow.com

github.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の方が若干シンプルかなと思う