@jameslnewell/cancelable-promise
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

@jameslnewell/cancelable-promise

A cancelable promise implementation.

Why?

Unlike other libraries:

  • its your choice whether promises are resolved OR rejected
  • .cancel() is chainable e.g. promise.then(...).cancel()

Installation

yarn add @jameslnewell/cancelable-promise 

Usage

import CancelablePromise from '@jameslnewell/cancelable-promise';

const start = Date.now();

function delay() {
  return new CancelablePromise((resolve, reject) => {
    const timeout = setTimeout(resolve, 10000);
    return () => {
      clearTimeout(timeout);
      resolve();
    };
  });
}

function exit() {
  process.stdin.end();
  const finish = Date.now();
  const elapsed = (finish - start) / 1000;
  console.log(`Waited ${elapsed} seconds.`);
}

console.log('Waiting 10s before exiting. Press RETURN to exit now.');

const waiting = delay().then(exit, exit);

process.stdin.on('data', data => {
  waiting.cancel();
});

Readme

Keywords

none

Package Sidebar

Install

npm i @jameslnewell/cancelable-promise

Weekly Downloads

3

Version

1.0.2

License

MIT

Unpacked Size

7.21 kB

Total Files

5

Last publish

Collaborators

  • jameslnewell