Git Product home page Git Product logo

promise-chain-settled's Introduction

npm version

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.

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.

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.

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

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

promise-chain-settled's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar github-actions[bot] avatar tjenkinson avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.