Add isPromise snippet

This commit is contained in:
Angelos Chalaris
2017-12-31 14:20:21 +02:00
parent dab931d9ba
commit 0440bc9d8c
2 changed files with 17 additions and 0 deletions

16
snippets/isPromise.md Normal file
View File

@ -0,0 +1,16 @@
### isPromise
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 isPromise = obj =>
obj !== null && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
```
```js
isPromise({then:function () {return ''}}); // true
isPromise(null); // false
isPromise({}); // false
```

View File

@ -73,6 +73,7 @@ isFunction:utility
isNull:utility
isNumber:utility
isPrime:math
isPromise:utility
isString:utility
isSymbol:utility
JSONToDate:date