@nexustools/promised
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Package Version Build Status Coverage Status Apache License 2.0

@nexustools/promised

An extension to Promise that removes the executor requirement, is much more versatile, and adds .reject and .accept methods.

Install

npm install @nexustools/promised

API

export = class Promised<V> extends Promise<V> {
  constructor(executor?: PromiseLike<V> | ((this: Promised<V>, resolve: Promised.Resolve<V>, reject: Promised.Reject, finished: () => boolean) => void), dispose?: (value: V) => void): Promised<V>;

  /**
   * Method used to dispose of unused resolved values.
   **/
  dispose: (value: V) => void;

  /**
   * Whether or not this Promised has finished (resolved or rejected).
  **/
  readonly finished: boolean;

  /**
   * Resolves a value, or another promise to continue.
   *
   * @returns true if resolved (!this.finished), undefined otherwise.
   */
  resolve(value: V | PromiseLike<V> | ((this: Promised<V>, resolve: Promised.Resolve<V>, reject: Promised.Reject, finished: () => boolean) => void)): void | true;

  /**
   * Reject and optionally specify why.
   *
   * @returns true if a reject can still be done (!this.finished), nothing (false) otherwise.
   */
  reject(reason?: any): void | true;
}

Usage

import Promised = require("@nexustools/promised");

let promised = new Promised(/* Optionally specify an executor, or another promise here */);

promised.despite = (value) => {
  // Handle values which resolve, after finish
};

promised.resolve(value); // Resolve
promised.reject(reason); // Reject

promised.then((val) => {
  // Handle resolve
}).catch((reason) => {
  // Handle reject
}).finally(() => {
  // Handle either
});

license

Licensed under Apache License 2.0

Readme

Keywords

none

Package Sidebar

Install

npm i @nexustools/promised

Weekly Downloads

1

Version

1.0.0

License

Apache-2.0

Unpacked Size

137 kB

Total Files

32

Last publish

Collaborators

  • ktaeyln