Files
30-seconds-of-code/snippets/is-promise-like.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

29 lines
742 B
Markdown

---
title: Value is promise-like
tags: type,function,promise
cover: digital-nomad-13
firstSeen: 2017-12-31T14:25:43+02:00
lastUpdated: 2020-10-20T23:02:01+03:00
---
Checks if an object looks like a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
- 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
```