Files
30-seconds-of-code/snippets/isPromiseLike.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

723 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
isPromiseLike type,function,promise,intermediate 2017-12-31T14:25:43+02:00 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