请注意
Promise.all(),创建诺言本身不会触发诺言开始工作。
考虑到这一点,一种解决方案是检查承诺何时得到解决,是否应该启动新的承诺,或者您是否已经达到极限。
但是,实际上没有必要在这里重新发明轮子。您可以用于此目的的一个库是
es6-promise-pool。从他们的例子:
// On the Web, leave out this line and use the script tag above instead. var PromisePool = require('es6-promise-pool')var promiseProducer = function () { // Your pre 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)})


