Files
30-seconds-of-code/snippets/js/s/is-promise-like.md
2023-05-07 16:07:29 +03:00

743 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Value is promise-like snippet javascript
type
function
promise
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, its typeof matches either object or function and if it has a .then property, which is also a function.
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