Value is promise-like

OSCC · JavaScript, Type, Function, Promise · Oct 20, 2020

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

Recommended snippets

  • Value is object-like

    JavaScript, Type

    Checks if a value is object-like.

  • Collection is empty

    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.

  • Value is async function

    JavaScript, Type

    Checks if the given argument is an async function.