Catalog npm
Back to packageRELEASE DETAILS

1.2.2

npm

@0x2e757/promise-ext

Promises extended

cancelablecancellableextendedpromisepromisestypescriptfastrejectableresolvable
Published12 Jul 2018
Direct dependencies1
Transitive dependenciesNot calculated

README

Current package documentation

PromiseExt

Extension of the native JavaScript Promise class, providing additional functionality for managing promise state and handling common use cases such as timeouts, cancellations, and state inspections. This package is designed specifically for TypeScript-based projects.

The PromiseExt class handles the native promise as a private property instead of extending the Promise class. This approach prevents issues that can arise from third-party library manipulations with the native Promise class and its methods.

Code must target ES2021 (or include the lib) or newer because of Promise.any method.

Features

Timeout: Create promise that resolve after a specified delay.

State Management: Track the state of promise (pending, resolved, rejected or canceled).

Cancellation: Cancel promise at any point.

Resolve/Reject as properties: Methods to resolve/reject promise at any point outside of executor (can be omitted).

Installation

Node.js

npm install @0x2e757/promise-ext
import { PromiseExt } from "@0x2e757/promise-ext";

Deno

deno add @0x2e757/promise-ext
import { PromiseExt } from "@0x2e757/promise-ext";

or

import { PromiseExt } from "jsr:@0x2e757/promise-ext";

Examples

Basic

import { PromiseExt } from "@0x2e757/promise-ext";

const promise = new PromiseExt((resolve) => {
  resolve("Hello, world!");
});

promise.then(console.log); // Output: "Hello, world!"

Timeout

import { PromiseExt } from "@0x2e757/promise-ext";

const promise = PromiseExt.timeout(1000, "Hello, world!");

promise.then(console.log); // Output: "Hello, world!" after 1 second

Creating and Managing Promises

import { PromiseExt } from "@0x2e757/promise-ext";

const promise = new PromiseExt<string>();

promise.then(console.log);
console.log(promise.state); // Output: "pending"

promise.resolve("Hello, world!"); // Output: "Hello, world!"
console.log(promise.state); // Output: "resolved"

Canceling Promises

import { PromiseExt } from "@0x2e757/promise-ext";

const promise = new PromiseExt<string>((resolve, reject, cancel) => {
  setTimeout(resolve, 2000, "Success"); // Outputs warning that promise was canceled
});

promise.cancel();

console.log(promise.state); // Output: "canceled"

API

Static Properties

static logger?: Logger - Logger function for logging warnings. Set to undefined to suppress warnings. Uses console.warn by default.

Static Methods

timeout<T>(delay?: number, value?: T): PromiseExt<T> - Creates a promise that resolves after the specified delay.

Instance Properties

state: PromiseState — The current state of the promise (pending, resolved, rejected or canceled).

resolve: (value: T) => void — Wrapper for resolving the promise.

reject: (reason: any) => void — Wrapper for rejecting the promise.

cancel: () => void — Wrapper for canceling the promise.

isPending: boolean — Checks if the promise is pending.

isResolved: boolean — Checks if the promise is resolved.

isRejected: boolean — Checks if the promise is rejected.

isCanceled: boolean — Checks if the promise is canceled.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 1

Native requirements
1 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^2.9.1development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "ef6dc30a2c1b18e542eda69023dd8e6d9ae2e4a8",
  "tarball": "https://registry.npmjs.org/@0x2e757/promise-ext/-/promise-ext-1.2.2.tgz",
  "fileCount": 11,
  "integrity": "sha512-JGguX49xZEOzCJPcGESW8ZxOd11UFFZqo2bf7WeNexbmESKlPlg31YOv5JsAEVETbyRXDRp2Io6oC4/4oDUmCw==",
  "signatures": [
    {
      "sig": "MEQCIEvbzkK28wdndllURXtI7TjSVjyZv/LNTFnQ5L33rGtiAiAbg/Mq02KdNFdqUzTmcyspHVjbdXDS6e+DGSUZgasP5w==",
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"
    }
  ],
  "unpackedSize": 63245,
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbR2TDCRA9TVsSAnZWagAAAVQP/2UTbFf1aCRq/JjIpReT\n7ZG3+eqbgj0286Nq6q4CI/BOGR/DfIOu3JC0JJnFhg5fW4RNh5A/eVy3lUve\nJkQtrg6v9pPUuH5YxDQvB9pSjIj0LKq+QePFrpCEpRJoRH/plLHdnjGpc642\nGWPo6J88vxx+zl1VZl2mm0/tvEUtQazhHT0veuN9ik9n/2XVaERc51dNbKWL\nFx3W0kzalwKW/DZ2D1yPNaVKPYZD8g7FxDSaX/Ppd841Lp0aTPInuJRpLhM3\n5VHEM/TPzJGSafWzRI1wos86y+J1MXf/ZmOJ6Xj6O0s4i88Rd+kW34reJrgH\nOxbZxfvuJX+bd+d+zj5NHLM/BvLgo68y4cEn1+t3Uamx0kwRap9NxLWD5HOB\nZSohZsllADpYUJGuuvM8Ia3H/kRXqIZjsJRls5CaqoArfwNXiwA8D8TL8jLt\nMDocNyxa2dn5uxnuePkjff3k+JdEv13bGMDR/fYsiuunkqoblxbRzv0VmvFM\nQrKOKmpRmxp8aCVehKHl+yrsHPfgCF992icGqlI5E5T6n1cusnirD2dr/V9P\n2HB9V085LP/G1J26/32tCP6rhwlcDxXWpHlb1YeQOh0OBSqTTQQKqyxt256K\nTPD4nHNN4Qnpl6ItEGbBxoMQvcTGyGXAo2Sjdk5sJNxwvzTJNC+PT1SX+Nl7\nRSum\r\n=reYI\r\n-----END PGP SIGNATURE-----\r\n"
}

Repository

PENDING
0x2e757/promiseext

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 22:34 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:da7282c22db80a214477e07d922bd7667d058a16da4a6bf214907da1cee72ff3

Other collected versions

4.1.110 Jun4.1.010 Jun4.0.18 Jun4.0.07 Jun3.0.26 Jun3.0.15 JunView all collected versions →