一个易用的promise队列
中文 English
这是一个并发的promise队列,并且可以随时暂停
当并发数目被设置为1时(默认值),这就是一个先进先出(FIFO)队列
你可以把promises放到队列中。只有X个promise可以同时执行。
你可以在任何时候暂停/恢复队列。当队列被暂停的时候,已经开始执行的promise任然会继续完成执行。
$ npm install easy-promise-queue
commonJS:
const PromiseQueue = require("easy-promise-queue").default;
es2015:
import PromiseQueue from 'easy-promise-queue';
let pq = new PromiseQueue({concurrency: 1});
pq.add(() => {
return new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('task 1');
resolve();
}, 1000)
});
});
pq.add(() => {
return new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('task 2');
resolve();
}, 1000)
});
});
// syntax sugar:
pq.add([promiseThunk, promiseThunk, promiseThunk]);
// is equal to:
pq.add(promiseThunk).add(promiseThunk).add(promiseThunk);
// is equal to:
pq.add(promiseThunk);
pq.add(promiseThunk);
pq.add(promiseThunk);
//The added promises will be executed one by one.
...
pq.pause();
// you can still add promise, however none of them will run.
pq.resume();
// Promises will resume to run.