Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tjenkinson/promise-chain-settled
Provides a way of knowing when a promise chain is settled. Useful for testing.
https://github.com/tjenkinson/promise-chain-settled
fulfilled promise promise-chain rejected resolved settled testing
Last synced: 18 days ago
JSON representation
Provides a way of knowing when a promise chain is settled. Useful for testing.
- Host: GitHub
- URL: https://github.com/tjenkinson/promise-chain-settled
- Owner: tjenkinson
- License: mit
- Created: 2020-07-01T21:03:48.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-03-29T18:06:15.000Z (8 months ago)
- Last Synced: 2024-10-23T22:23:48.765Z (22 days ago)
- Topics: fulfilled, promise, promise-chain, rejected, resolved, settled, testing
- Language: TypeScript
- Homepage:
- Size: 557 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![npm version](https://badge.fury.io/js/promise-chain-settled.svg)](https://badge.fury.io/js/promise-chain-settled)
# promise-chain-settled
Provides a way of knowing when a promise chain is settled. Useful for testing.
## Installation
```sh
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.
```ts
const promise = new Promise(() => {});
const wrapped = wrap(promise);
console.log(wrapped.numPending()); // 1
promise.then(() => {});
console.log(wrapped.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.
```ts
const promise = Promise.resolve();
const wrapped = wrap(promise);
console.log(wrapped.isChainSettled()); // false
await promise;
console.log(wrapped.isChainSettled()); // true
promise.then(() => {});
console.log(wrapped.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.
```ts
const promise = Promise.resolve();
const wrapped = wrap(promise);
wrapped.onChange(() => {
// first call: new numPending() 2
// second call: new numPending() 1
// third call: new numPending() 0
console.log('new numPending()', wrapped.numPending());
});
promise.then(() => {});
```## Example
```ts
import { wrap } from 'promise-chain-settled';const promise = Promise.resolve();
// modify the promise so that we can keep track of everything in the chain
const wrapped = wrap(promise);promise
.then(() => {
return somethingAsync();
})
.then(() => {
return somethingElseAsync();
})
.then((something) => {
console.log('Done');
});wrapped.whenChainSettled().then(() => {
console.log('Chain settled');
});
```would log
```
Done
Chain settled
```