UNPKG

froebel

Version:
73 lines (64 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _noop = _interopRequireDefault(require("./noop")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Creates a `queue` function that accepts a function as it's only parameter. * When `queue` is invoked, the passed in function is executed after the last * function passed to `queue` has finished executing. The `queue` function * returns the result of the passed in function asynchronously. * * Reading `queue.done` is `true` if no functions are currently executing / * scheduled and otherwise a promise that resolves once the last function has * stopped executing and no futher functions are queued. * * @example * ``` * const queue = createQueue() * * queue(async () => { * console.log('start a') * await delay() * return 'end a' * }).then(console.log) * * queue(async () => { * console.log('start b') * await delay() * return 'end b' * }).then(console.log) * * queue(async () => { * console.log('start c') * await delay() * return 'end c' * }).then(console.log) * * await queue.done * * // start a * // end a * // start b * // end b * // start c * // end c * ``` */ const createQueue = () => { let last = null; const queued = fun => last = (last ?? Promise.resolve()).catch(_noop.default).then(fun).finally(() => { last = null; }); return Object.defineProperty(queued, "done", { get: () => { var _last; return ((_last = last) === null || _last === void 0 ? void 0 : _last.catch(_noop.default).then(_noop.default)) ?? true; } }); }; var _default = createQueue; exports.default = _default; module.exports = Object.assign(exports.default || {}, exports);