JavaScript, Type, Function, Promise

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';
  then: function() {
    return '';
}); // true
isPromiseLike(null); // false
isPromiseLike({}); // false

Recommended snippets

  • isObjectLike

    JavaScript, Type

    Checks if a value is object-like.

  • shank

    JavaScript, Array

    Has the same functionality as Array.prototype.splice(), but returning a new array instead of mutating the original array.

  • isEmpty

    JavaScript, Type

    Checks if the a value is an empty object/collection, has no enumerable properties or is any type that is not considered a collection.