Skip to content
VersionSize

isPromise

Checks if a value is a Promise.

Implementation

View Source Code
ts
import { typeOf } from './typeOf';

/**
 * Determines if the passed value is a promise.
 *
 * @example
 * ```ts
 * isPromise(new Promise((resolve, reject) => {})); // true
 * isPromise(async () => {}); // true
 * isPromise(() => {}); // false
 * ```
 *
 * @param arg - The argument to be checked.
 *
 * @returns `true` if the value is a promise, else `false`.
 */
export function isPromise(arg: unknown): arg is Promise<unknown> {
  return typeOf(arg) === 'promise';
}

export const IS_PROMISE_ERROR_MSG = 'Expected a promise';

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