is-promise
is-promise is a minimalist utility that determines if an object adheres to the Promises/A+ specification by duck-typing, specifically checking for the presence and function type of a `then` method. The package is currently at version 4.0.0, which primarily introduced a breaking change for TypeScript users regarding `PromiseLike` inference. While it doesn't follow a strict, rapid release cadence, major versions address significant compatibility updates, particularly concerning module resolution in Node.js environments and TypeScript definitions. Its key differentiator is its small footprint and focused API, providing a reliable and agnostic way to identify promise-like objects, useful in contexts where various promise implementations or thenables might be encountered, without relying on a global `Promise` constructor.
Common errors
-
TypeError: isPromise is not a function
cause Attempting to use `isPromise` as a named import (e.g., `import { isPromise } from 'is-promise';`) in an ES Module environment.fixChange the import statement to use the default export: `import isPromise from 'is-promise';` (applicable for v3.0.0 and above). -
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not exported from package 'is-promise'
cause Trying to `require` or `import` a subpath (like the package's `package.json` file) directly from `is-promise` after v3.0.0, which restricted exports.fixAccess to internal package files is no longer supported. If you need information from `is-promise`'s `package.json`, consider hardcoding it or finding another way to obtain it that doesn't involve directly accessing its internal file structure.
Warnings
- breaking For TypeScript users, `is-promise` v4.0.0 changed its internal type inference from `Promise` to `PromiseLike`. This change correctly reflects that the library checks for any object with a `.then()` method, not strictly instances of the global `Promise` constructor. Type inference in user code might behave differently.
- breaking Version 3.0.0 introduced restricted "exports" in `package.json` for Node 14+ environments. This means that only the public API (the main `isPromise` function) is accessible. Directly requiring or importing internal files, such as the `package.json` file of the package itself, is no longer supported and will result in module not found errors.
- breaking Since v3.0.0, `is-promise` uses default exports in ES Modules environments. If you were previously using named imports (e.g., `import { isPromise } from 'is-promise'`) with earlier ESM-enabled versions, this pattern will no longer work and will cause runtime errors.
- breaking Versions 2.2.0 and 2.2.1 of `is-promise` were officially marked as broken due to issues with their initial attempts at adding ES Module support, which led to incorrect module resolution and other problems. These versions should be avoided.
Install
-
npm install is-promise -
yarn add is-promise -
pnpm add is-promise
Imports
- isPromise
import { isPromise } from 'is-promise';import isPromise from 'is-promise';
- isPromise
const isPromise = require('is-promise');
Quickstart
import isPromise from 'is-promise';
async function processValue(value: unknown): Promise<string> {
if (isPromise(value)) {
console.log('Value is a promise, awaiting...');
const result = await value;
return `Resolved from promise: ${result}`;
} else if (typeof value === 'string') {
console.log('Value is a string.');
return `Direct string: ${value}`;
} else if (typeof value === 'object' && value !== null && 'then' in value && typeof (value as any).then === 'function') {
console.log('Value is promise-like (has a then method), awaiting...');
const result = await (value as PromiseLike<any>);
return `Resolved from promise-like: ${result}`;
} else {
console.log('Value is neither a promise nor a string.');
return 'Unknown value type.';
}
}
(async () => {
console.log(await processValue(Promise.resolve('Hello from a real promise')));
console.log(await processValue({ then: (resolve: (arg0: string) => void) => setTimeout(() => resolve('Hello from a custom thenable'), 100) }));
console.log(await processValue('Hello from a string'));
console.log(await processValue(null));
console.log(await processValue({ then: true })); // 'then' is not a function, so not a promise.
})();