Function: isPromise()
isPromise<
T
>(val
):val is Promise<T>
Type guard to check if a value is a Promise-like object (thenable). Checks for the presence of 'then' and 'catch' methods.
Type Parameters
T
T
= unknown
The type that the Promise resolves to
Parameters
val
unknown
The value to check
Returns
val is Promise<T>
True if the value has Promise-like behavior
Example
typescript
const asyncFn = async () => 'result'
const promise = Promise.resolve('value')
const thenable = { then: () => {}, catch: () => {} }
if (isPromise(promise)) {
// TypeScript knows this is a Promise
promise.then(value => console.log(value))
}