Is there a isPromise proposal?

In relation to this -- https://github.com/then/is-promise/issues/20

Do we have any proposal for an isPromise static function on the Promise constructor?
Right now, there are many ways to test for whether an object is just "thenable" or an actual native promise.

For example --

function isPromise(obj) {
  return Promise.resolve(obj) === obj;
}

function isPromise(obj) {
  return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
}

function isPromise(obj) {
  return obj instanceof Promise;
}

I am not sure which promise check is better, but I've seen all 3 in places.

Is there any exisiting proposal of this? Is it even a good idea? Would like to know more.

Thanks for your time!

Hi,
I don't know of any such proposal, the example codes you've given seem sufficient for all the common use cases.

It would depend on your goal of course. (And tbh I'd dub the second one isThenable, not isPromise). So, for what do you need to check whether a value is a promise?

1 Like

The first one is the only correct one.

Generally speaking, you shouldn’t need to check if something is a promise. Instead, await or Promise.resolve it, and then you don’t need to care :-)

3 Likes

Agreed. The rare exceptions that do exist lie almost exclusively within frameworks and the like, and they care about if there's a .then method, not if it's a native promise.

Edit: Rip out a bunch of incorrect claims I spent too much time writing.

I am also trying to find out if somebody wants to help implement convenient value checks as described in this article and this proposal. Maybe Promise could be similarly implemented?