UNPKG

async-promise-queue

Version:

wrapper around async.queue to make some common usages simpler

39 lines (33 loc) 953 B
'use strict'; var debug = require('debug')('async-promise-queue:'); // helper function that promisifies the queue module.exports = function queue(worker, work, concurrency) { debug('started, with concurrency=' + concurrency) return new Promise(function(resolve, reject) { if (work.length === 0) { resolve(); } else { var q = require('async').queue(worker, concurrency); var firstError; q.drain = resolve; q.error = function(error) { if (firstError === undefined) { // only reject with the first error; firstError = error; } // don't start any new work q.kill(); // but wait until all pending work completes before reporting q.drain = function() { reject(firstError); }; }; q.push(work); } }); }; Object.defineProperty(module.exports, 'async', { get: function() { return require('async'); } });