Skip to content

isPromiseLike

Checks if a value is a thenable (has a .then() method).

Looser than isPromise: accepts any object or function with a then method, including non-standard Promise implementations without .catch(). Follows the Promise/A+ specification for thenables.

Available since v2.0.3

import { isPromiseLike } from '@helpers4/type';
isPromiseLike(value: unknown): value is PromiseLike<unknown>
ParameterTypeDescription
valueunknownThe value to check

value is PromiseLike<unknown> — true if value is a PromiseLike (thenable)

Returns true for native Promises and any object with a .then() method.

isPromiseLike(Promise.resolve(1))    // => true
isPromiseLike({ then: () => {} })    // => true   (thenable)
isPromiseLike(42)                    // => false
isPromiseLike(null)                  // => false
isPromiseLike({ then: 'not-a-fn' }) // => false

Handle both Promises and thenables in a utility

Section titled “Handle both Promises and thenables in a utility”

Use isPromiseLike to accept any thenable, not just native Promises.

function toPromise<T>(value: T | PromiseLike<T>): Promise<T> {
  if (isPromiseLike(value)) return Promise.resolve(value);
  return Promise.resolve(value);
}

View source on GitHub