@prexjs/promise
v1.0.0
Published
Lightweight, zero-dependency type guard utility to test if a value is a Promise or Thenable.
Readme
@prexjs/promise
Lightweight, zero-dependency type guard utility to test if a value is a Promise or Thenable.
@prexjs/promise provides an ultra-fast, robust TypeScript type guard isPromise(val) to determine if a value is an ES6 Promise, async thenable, or third-party promise (Bluebird, Q, etc.).
Features
- Validates whether an object or function qualifies as a Promise/A+ thenable.
- TypeScript user-defined type guard narrowing (
value is PromiseLike<T>). - Dual ES Module (
.mjs) and CommonJS (.cjs) support with TypeScript definitions. - Zero runtime dependencies.
Installation
Install using your preferred package manager:
# npm
npm install @prexjs/promise
# pnpm
pnpm add @prexjs/promise
# Yarn
yarn add @prexjs/promise
# Bun
bun add @prexjs/promiseQuick start
import isPromise from '@prexjs/promise';
console.log(isPromise(Promise.resolve('antigravity'))); // true
console.log(isPromise({ then: (resolve) => resolve(1) })); // true
console.log(isPromise('not a promise')); // false
console.log(isPromise(null)); // falseBoth default and named exports are supported:
import isPromise, { isPromise as namedIsPromise } from '@prexjs/promise';Examples
Handling synchronous or asynchronous results
import isPromise from '@prexjs/promise';
function handle(result: unknown) {
if (isPromise(result)) {
result.then((val) => {
console.log('Async resolved:', val);
});
} else {
console.log('Sync result:', result);
}
}Compatibility
@prexjs/promise runs anywhere ECMAScript executes (Node.js, Deno, Bun, and all browsers).
License
This project is licensed under the MIT License. See LICENSE for the full license text.
Related projects
- prex.js — prex.js monorepo and ecosystem.
