isPromise
Determine if a value is a Promise or has a `then` method
Usage
The isPromise
function checks if a value is “Promise-like” by determining if it has a then
method.
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:
- You need to quickly check if a value is thenable without resolving it.
- Performance is critical, and you want to avoid the overhead of
Promise.resolve
. - 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.