Skip to content
VersionSize

isPromise

Checks if a value is a Promise.

Source Code

View Source Code
ts
/**
 * Determines if the passed value is a Promise (or any thenable).
 *
 * @example
 * ```ts
 * isPromise(new Promise((resolve) => resolve(1))); // true
 * isPromise(asyncFn()); // true  (calling it returns a Promise)
 * isPromise(async () => {}); // false (the function itself is not a Promise)
 * isPromise(() => {}); // false
 * ```
 *
 * @param arg - The argument to be checked.
 *
 * @returns `true` if the value is a thenable, else `false`.
 */
export function isPromise(arg: unknown): arg is Promise<unknown> {
  return typeof arg === 'object' && arg !== null && typeof (arg as { then?: unknown }).then === 'function';
}

API

ts
function isPromise(value: unknown): value is Promise<unknown>;

Parameters

  • value: The value to check

Returns

  • true if the value is a Promise, false otherwise

Examples

Basic Usage

ts
import { isPromise } from '@vielzeug/toolkit';

isPromise(Promise.resolve(42)); // true
isPromise(42); // false
isPromise((async () => {})()); // true

Implementation Notes

  • Checks for the presence of a .then method
  • Useful for type guards and async code

See Also