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
trueif the value is a Promise,falseotherwise
Examples
Basic Usage
ts
import { isPromise } from '@vielzeug/toolkit';
isPromise(Promise.resolve(42)); // true
isPromise(42); // false
isPromise((async () => {})()); // trueImplementation Notes
- Checks for the presence of a
.thenmethod - Useful for type guards and async code
See Also
- isFunction: Check if value is a function
- isObject: Check if value is an object
- typeOf: Get the type of any value