743 B
743 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |||
|---|---|---|---|---|---|---|---|---|
| Value is promise-like | snippet | javascript |
|
digital-nomad-13 | 2020-10-20T23:02:01+03:00 |
Checks if an object looks like a Promise.
- Check if the object is not
null, itstypeofmatches eitherobjectorfunctionand if it has a.thenproperty, which is also afunction.
const isPromiseLike = obj =>
obj !== null &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function';
isPromiseLike({
then: function() {
return '';
}
}); // true
isPromiseLike(null); // false
isPromiseLike({}); // false