Orderly Queue
Implementation of a promise-based FIFO queuing system using ES2017 async generators.
npm:
npm i orderly-queue -S
Babel:babel-plugin-syntax-async-generators
- Takes a function that returns a promise (or
Promise.all
) - Invokes the promise and
yield
s the eventual result - Awaits the completion of the task before beginning the next
- Implements a pseudo-observable for
next
anderror
- Passes the returned
props
along to the next task
Usage
; const queue = ; queue;queue;queue; // > ['Blueberries']// > ['Blueberries', 'Apples']// > ['Blueberries', 'Apples', 'Bananas']// > ['Blueberries', 'Apples', 'Bananas', 'Raspberries']
Each task will wait before the completion of the current task, meaning you can safely assume the order of fruits
no matter how long it takes for a single task to complete.
Errors
Any errors that are raised will be passed to the error
function, however the items in the queue will continue to be invoked one-at-a-time passing in the props
from the last successful invocation.
; const queue = ; queue;queue;queue;queue; // > ['Blueberries']// > ['Blueberries', 'Apples']// > Error: Fruitless...// > ['Blueberries', 'Apples', 'Bananas']// > ['Blueberries', 'Apples', 'Bananas', 'Raspberries']
In cases where you wish to end the iterator early you can invoke the abort
method — perhaps in response to an error being raised. Any queued tasks will not be run.
const queue = ; // ... queue;