0.4.1 • Published 1 year ago

@webreflection/promise v0.4.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

@webreflection/promise

Coverage Status build status

Social Media Photo by Andrew Petrov on Unsplash

Abortable and Resolvable Promises.

This module exposes a dropin replacement for both Promise and AbortController with the following extra features:

  • the Promise accepts an optional argument that could be a controller or an object carrying an AbortSignal with it.
  • the AbortController exposes a resolve(value) method so that not only it's possible to abort a Promise, it's also possible to resolve it, still using the security and ownership guards that AbortSignal offers.

As a matter of fact, the Promise literally extends the builtin Promise and simply adds a listener to the optional signal, while the AbortController extends the builtin class too, exposing the extra resolve(value) method.

// const {AbortController, Promise} = require('@webreflection/promise');
import {AbortController, Promise} from '@webreflection/promise';

const controller = new AbortController;
const promise = new Promise(
  // optionally receive a signal as third argument
  // if passed via controller or object
  (resolve, reject, signal) => {
    const t = setTimeout(resolve, 1000, 'automatically');
    // optional return a callback to cleanup whenever
    // the promise is either resolved or aborted
    return () => { clearTimeout(t) }
  },
  controller
);

// resolve the promise ahead of time
controller.resolve('manually');

// or abort the promise with a reason
constroller.abort('but why?!');

Every operation is fully based on default AbortController standard behavior: no hacks, no tricks, just a convenient utility suitable for all occasions.

0.4.1

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.0

1 year ago