@woubuc/deferred
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Deferred

A Promise that can be resolved externally

npm MIT licensed install size Typescript type definitions included

Installation

yarn add @woubuc/deferred

The library is written in Typescript to types are included.

Usage

import Deferred from '@woubuc/deferred';

// Use it as a regular promise
let promise = new Deferred((resolve, reject) => {
    resolve('done');
});

// Or resolve it externally
promise.resolve('done');

// Or omit the promise body altogether
let deferred = new Deferred();
deferred.resolve('done');

API

let deferred = new Deferred<T>(promiseCallback);

Creates a new Deferred promise. Takes a (resolve, reject) callback just like a regular promise.

  • <T>: Type of the promise resolve value
deferred.resolve(value : T);

Resolves the promise with value

deferred.reject(error ?: any);

Rejects the promise with an optional error.

Notes

  • Extends the native Promise object, so it should be fully compatible with regular promises
  • Resolving and rejecting follows the same rules as with regular promises (i.e. a promise can only be settled once)
  • Resolving the promise externally will not stop or code inside the constructor callback from executing

Package Sidebar

Install

npm i @woubuc/deferred

Weekly Downloads

14

Version

2.0.0

License

MIT

Unpacked Size

14.2 kB

Total Files

9

Last publish

Collaborators

  • woubuc