isPromise()
isPromise<
T>(value):value is Promise<T>
Checks if a value is a Promise or Promise-like object (thenable).
Type Parametersโ
T: T = anyโ
The type of the value the promise resolves to.
Parametersโ
value: unknownโ
The value to check.
Returns: value is Promise<T>โ
true if the value is a Promise or thenable, false otherwise.
Sinceโ
2.0.0
Exampleโ
isPromise(new Promise(() => {})); // => true
isPromise({ then: () => {} }); // => true
isPromise(async () => {}); // => false (it's a function that returns a promise)
isPromise({}); // => false
How it works?โ
Type guard that checks if a value is a Promise.
Type Narrowingโ
Common Checksโ
| Value | Result |
|---|---|
Promise.resolve(1) | true |
new Promise(() => {}) | true |
async () => {} | false (function) |
(async () => {})() | true (called) |
{ then: () => {} } | false (thenable) |
Noteโ
This checks for native Promise instances. Thenables (objects with .then()) are not detected.
Use Casesโ
Detect async operations ๐โ
Check if a value behaves like a Promise (thenable). Essential for handling mixed synchronous and asynchronous APIs.
if (isPromise(result)) {
await result;
}
true
false (function)