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
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