diff --git a/snippets/isPromiseLike.md b/snippets/isPromiseLike.md new file mode 100644 index 000000000..d7bb9a75b --- /dev/null +++ b/snippets/isPromiseLike.md @@ -0,0 +1,16 @@ +### isPromiseLike + +Returns `true` if an object looks like a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), `false` otherwise. + +Check if the object is not `null`, its `typeof` matches either `object` or `function` and if it has a `.then` property, which is also a `function`. + +```js +const isPromiseLike = obj => + obj !== null && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'; +``` + +```js +isPromiseLike({then:function () {return ''}}); // true +isPromiseLike(null); // false +isPromiseLike({}); // false +``` diff --git a/tag_database b/tag_database index 14b1482d7..82a224942 100644 --- a/tag_database +++ b/tag_database @@ -75,6 +75,7 @@ isFunction:utility isNull:utility isNumber:utility isPrime:math +isPromiseLike:utility isString:utility isSymbol:utility isValidJSON:utility