Merge pull request #436 from Chalarangelo/isPromise
[FEATURE] Add isPromise snippet
This commit is contained in:
16
snippets/isPromiseLike.md
Normal file
16
snippets/isPromiseLike.md
Normal file
@ -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
|
||||||
|
```
|
||||||
@ -75,6 +75,7 @@ isFunction:utility
|
|||||||
isNull:utility
|
isNull:utility
|
||||||
isNumber:utility
|
isNumber:utility
|
||||||
isPrime:math
|
isPrime:math
|
||||||
|
isPromiseLike:utility
|
||||||
isString:utility
|
isString:utility
|
||||||
isSymbol:utility
|
isSymbol:utility
|
||||||
isValidJSON:utility
|
isValidJSON:utility
|
||||||
|
|||||||
Reference in New Issue
Block a user