froebel
Version:
TypeScript utility library
49 lines (48 loc) • 1.27 kB
TypeScript
import type { MakeProm, λ } from "./types";
/**
* 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
* ```
*/
declare const createQueue: () => Queue;
export default createQueue;
declare type Queue = (<T extends λ<[], unknown>>(fun: T) => MakeProm<ReturnType<T>>) & {
done: Promise<void> | true;
};