binding-promise

1.0.1 • Public • Published

npm badge

binding-promise

A native binding for Node.js Promise objects.

You can access [[PromiseState]] internal slot synchronously using binding-promise.

Examples

import { isPending } from "binding-promise";

const promise = new Promise((resolve) => /* do something and resolve() */);
isPending(promise); // Promise is not resolved or rejected if true.
import { isFulfilled, isRejected } from "binding-promise";

const promise = new Promise((resolve, reject) => /* do something and call resolve or reject */);
await promise;

isFulfilled(promise); // Promise is resolved if true.
isRejected(promise); // Promise is rejected if true.
import assert from "assert";
import { getState, PromiseState } from "binding-promise";

assert(getState(Promise.resolve(1)) === PromiseState.FULFILLED);
assert(
  getState(new Promise<void>((resolve) => setTimeout(() => resolve(), 1))) ===
    PromiseState.PENDING
);

try {
  getState(1);
} catch (e) {
  assert(e instanceof TypeError);
}

Restrictions

  • Works only in Node.js environments.

License

MIT License

Copyright (c) 2024 Seunghoon Lee

Package Sidebar

Install

npm i binding-promise

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

7 kB

Total Files

12

Last publish

Collaborators

  • lshqqytiger