Skip to content

Instantly share code, notes, and snippets.

@mildronize
Created November 9, 2020 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mildronize/5f73230c8e2d692770d2a9326faefe2b to your computer and use it in GitHub Desktop.
Save mildronize/5f73230c8e2d692770d2a9326faefe2b to your computer and use it in GitHub Desktop.
Check if an Object is a Promise in JavaScript

Check if an Object is a Promise in JavaScript

https://debugpointer.com/check-if-an-object-is-a-promise/

Method 1

function isPromise(p) {
  return p && Object.prototype.toString.call(p) === "[object Promise]";
}

Method 2

function isPromise(value) {
  return Boolean(value && typeof value.then === "function");
}

Method 3

function isPromise(object) {
  if (Promise && Promise.resolve) {
    return Promise.resolve(object) == object;
  } else {
    throw "Promise not supported in your environment"; // Most modern browsers support Promises
  }
}

Method 4

function isPromise(obj) {
  return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment