promise-chain-settled
Provides a way of knowing when a promise chain is settled. Useful for testing.
Installation
npm install --save promise-chain-settled
or available on JSDelivr at "https://cdn.jsdelivr.net/npm/promise-chain-settled@1".
API
First you need to wrap the promise with wrap(promise)
.
This returns an object with the following properties:
numPending(): number
This returns the number of promises in the chain that are currently pending. It can increase at any time if new items are added.
;;console.logwrapped.numPending; // 1promise.then;console.logwrapped.numPending; // 2
isChainSettled(): boolean
Returns true
when numPending() === 0
meaning everything in the chain is settled. This can change at any time if new items are added.
;;console.logwrapped.isChainSettled; // falseawait promise;console.logwrapped.isChainSettled; // truepromise.then;console.logwrapped.isChainSettled; // false
whenChainSettled(): Promise
This returns a promise that resolves the next time isChainSettled()
goes from false
to true
.
onChange(listener: () => void): { remove: () => void }
This lets you add a listener that will be invoked whenever numPending()
changes.
;;wrapped.onChange;promise.then;
Example
; ; // modify the promise so that we can keep track of everything in the chain; promise .then .then .then; wrapped.whenChainSettled.then;
would log
Done
Chain settled