Skip to content

isPromise

Determine if a value is a Promise or has a `then` method

101 bytes

Usage

The isPromise function checks if a value is “Promise-like” by determining if it has a then method.

import * as
import _
_
from 'radashi'
import _
_
.
function isPromise(value: any): value is PromiseLike<unknown>
export isPromise

Returns true if the value is a Promise or has a then method.

@seehttps://radashi.js.org/reference/typed/isPromise

@example

isPromise(Promise.resolve(1)) // => true
isPromise({ then() {} }) // => true
isPromise(1) // => false

@version12.1.0

isPromise
({
then: () => void
then
: () => {} }) // => true
import _
_
.
function isPromise(value: any): value is PromiseLike<unknown>
export isPromise

Returns true if the value is a Promise or has a then method.

@seehttps://radashi.js.org/reference/typed/isPromise

@example

isPromise(Promise.resolve(1)) // => true
isPromise({ then() {} }) // => true
isPromise(1) // => false

@version12.1.0

isPromise
(new
var Promise: PromiseConstructor
new <unknown>(executor: (resolve: (value: unknown) => void, reject: (reason?: any) => void) => void) => Promise<unknown>

Creates a new Promise.

@paramexecutor A callback used to initialize the promise. This callback is passed two arguments: a resolve callback used to resolve the promise with a value or the result of another promise, and a reject callback used to reject the promise with a provided reason or error.

Promise
(() => {})) // => true
import _
_
.
function isPromise(value: any): value is PromiseLike<unknown>
export isPromise

Returns true if the value is a Promise or has a then method.

@seehttps://radashi.js.org/reference/typed/isPromise

@example

isPromise(Promise.resolve(1)) // => true
isPromise({ then() {} }) // => true
isPromise(1) // => false

@version12.1.0

isPromise
(
var Promise: PromiseConstructor

Represents the completion of an asynchronous operation

Promise
.
PromiseConstructor.resolve<number>(value: number): Promise<number> (+2 overloads)

Creates a new resolved promise for the provided value.

@paramvalue A promise.

@returnsA promise whose internal state matches the provided promise.

resolve
(1)) // => true
import _
_
.
function isPromise(value: any): value is PromiseLike<unknown>
export isPromise

Returns true if the value is a Promise or has a then method.

@seehttps://radashi.js.org/reference/typed/isPromise

@example

isPromise(Promise.resolve(1)) // => true
isPromise({ then() {} }) // => true
isPromise(1) // => false

@version12.1.0

isPromise
(
var Promise: PromiseConstructor

Represents the completion of an asynchronous operation

Promise
.
PromiseConstructor.reject<never>(reason?: any): Promise<never>

Creates a new rejected promise for the provided reason.

@paramreason The reason the promise was rejected.

@returnsA new rejected Promise.

reject
(new
var Error: ErrorConstructor
new (message?: string) => Error
Error
('nope'))) // => true
import _
_
.
function isPromise(value: any): value is PromiseLike<unknown>
export isPromise

Returns true if the value is a Promise or has a then method.

@seehttps://radashi.js.org/reference/typed/isPromise

@example

isPromise(Promise.resolve(1)) // => true
isPromise({ then() {} }) // => true
isPromise(1) // => false

@version12.1.0

isPromise
('hello') // => false
import _
_
.
function isPromise(value: any): value is PromiseLike<unknown>
export isPromise

Returns true if the value is a Promise or has a then method.

@seehttps://radashi.js.org/reference/typed/isPromise

@example

isPromise(Promise.resolve(1)) // => true
isPromise({ then() {} }) // => true
isPromise(1) // => false

@version12.1.0

isPromise
({}) // => false

This approach is useful for identifying objects that conform to the Promise interface without actually being instances of Promise. It’s particularly helpful in scenarios where:

  1. You need to quickly check if a value is thenable without resolving it.
  2. Performance is critical, and you want to avoid the overhead of Promise.resolve.
  3. You’re working with custom Promise implementations or third-party libraries that use Promise-like objects.

While Promise.resolve is generally recommended for handling both Promise and non-Promise values uniformly, isPromise can be preferable when you need to make decisions based on whether a value is Promise-like without actually resolving or chaining it. This can be especially useful in type-checking scenarios or when implementing control flow that depends on whether a value is immediately available or needs to be awaited.