JavaScript’s evolution has always intrigued me, and one of its most impressive aspects is the concept of Promises. A Promise in JavaScript represents a value that might not be available yet but will be available in the future, or never at all. It’s a fantastic feature, but it does come with its own set of complexities. One such challenge that I’ve often faced and seen others grapple with is how to check if an object is a Promise in JavaScript. This subtle yet crucial operation is our topic of discussion for today, and I can’t wait to dive into the nitty-gritty with you.
Are you stuck in a scenario in your JavaScript code where you have to check if an Object
in your code is a Promise
object or not?
Here are some methods to check if an Object is a Promise object.
Method 1 : Checking if Object.prototype.toString.call(p) === “[object Promise]”
This is the process of checking if the string version of prototype of the object p
is “[object Promise]”
function isPromise(p) {
return p && Object.prototype.toString.call(p) === "[object Promise]";
}
Here are some examples of various types of data and we see that only a JavaScript promise object returns a true
on calling the isPromise()
function.
// Tests
var pi = 3.14;
var name = "John";
var obj = { site: "debugpointer.com" };
var numbers = [1, 2, 3];
var prom = new Promise(function (resolve, reject) {
resolve();
});
isPromise(pi); // false
isPromise(name); // false
isPromise(obj); // false
isPromise(numbers); // false
isPromise(prom); // true
Method 2 : Checking if typeof value.then === ‘function’
In this method, we will check if .then
exists and if its a function.
function isPromise(value) {
return Boolean(value && typeof value.then === "function");
}
Now, let’s use the above isPromise()
method.
// Tests
var pi = 3.14;
var name = "Jogn";
var obj = { site: "debugpointer.com" };
var numbers = [1, 2, 3];
var prom = new Promise(function (resolve, reject) {
resolve();
});
isPromise(pi); // false
isPromise(name); // false
isPromise(obj); // false
isPromise(numbers); // false
isPromise(prom); // true
Source – https://github.com/graphql/graphql-js/blob/master/src/jsutils/isPromise.js
Method 3 : Checking if Promise.resolve(object) == object
Here is a very modular way where we check if .resolve()
is an object.
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
}
}
Now, let’s use the above isPromise()
method.
// Tests
var pi = 3.14;
var name = "Satvik";
var obj = { site: "debugpointer.com" };
var numbers = [1, 2, 3];
var prom = new Promise(function (resolve, reject) {
resolve();
});
isPromise(pi); // false
isPromise(name); // false
isPromise(obj); // false
isPromise(numbers); // false
isPromise(prom); // true
Credits – https://gist.github.com/MarkoCen/ec27b8cd42855fde8a245d43b7b081d0
Method 4 : Checking if obj is Object & obj.then is a function
In this method we conditionally check if obj
is an object and it has a function obj.then
.
function isPromise(obj) {
return (
!!obj &&
(typeof obj === "object" || typeof obj === "function") &&
typeof obj.then === "function"
);
}
Now, let’s use the above isPromise()
method.
// Tests
var pi = 3.14;
var name = "Satvik";
var obj = { site: "debugpointer.com" };
var numbers = [1, 2, 3];
var prom = new Promise(function (resolve, reject) {
resolve();
});
isPromise(pi); // false
isPromise(name); // false
isPromise(obj); // false
isPromise(numbers); // false
isPromise(prom); // true
Check how can you optimise conditional statements in Javascript.
To summarize, we have seem 4 ways to check if an Object is a Promise in Javascript. All of them can be used in your code. You can instead use TypeScript which should help with types as you code.
Our dive into the world of Promises has led us to a better understanding of how to check if an object is a Promise in JavaScript. I trust that our exploration together has not only provided you with a solution but also a deeper understanding of the nature of Promises in JavaScript. As we bring our discussion to a close, remember that mastering the subtleties like these are what can set you apart as a JavaScript developer. So, keep asking, keep learning, and remember that every Promise leads to an exciting revelation in the world of JavaScript!