How To Check If A Value Is A Promise Using JavaScript

Solution To Check If A Value Is A Promise Using JavaScript

There are many ways to check if a value is a promise using JavaScript. In this article, I will show you two methods: use the conditional statement and use the version of prototype chain to Check if a Value is a Promise. Let me clarify this in the next part of this article.

Check if a value is a promise using JavaScript

Use the version of the prototype chain to check

Syntax:

Object.prototype.toString.call(p)

Parameters – Description:

  • p: is the object that you want to check if it is a promise.

Example:

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

We try to use the function above to test with some different types of variables what the result of the return function is.

Example:

let myPromise = new Promise(function(myResolve, myReject) {
	let str = "LearnShareIT";

	if (str == "LearnShareIT") {
		myResolve("OK");
	} else {
		myReject("Error");
	}
}); // Promise

let myNum = 123; // Number
let myString = "LearnShareIT"; // String
console.log(isPromise(myPromise));
console.log(isPromise(myNum));
console.log(isPromise(myString));

Output:

true
false
false

The example name shows that the function we have just created works well. It can distinguish whether the passed parameter is a promise from which to output the corresponding result. However, this way, some people won’t understand. So next, I’ll show you another more straightforward way to understand.

Use Conditional (Ternary) Operator

We can use the ternary operator to Check if a Value is a Promise or not. First of all we will check if a Value is an Object and Value. Through a function we will determine whether the output is true or false after we check if the value is a promise or not.

Syntax:

(condition) ? x : y

Parameters – Description:

  • x: is the code to be called when the previous condition is true.
  • y: is the code to be called when the previous condition is false.

Example:

function isPromise(p) {
	var result;
  
	// Use Conditional (Ternary) Operator
	typeof p === "object" && typeof p.then === "function" ?
		(result = true) :
		(result = false);
	return result;
}

We will test the newly created function to see if it works properly.

Example:

let myPromise = new Promise(function(myResolve, myReject) {
	let str = "LearnShareIT";

	if (str == "LearnShareIT") {
		myResolve("OK");
	} else {
		myReject("Error");
	}
}); // Promise

let myNum = 123; // Number
let myString = "LearnShareIT"; // String
console.log(isPromise(myPromise));
console.log(isPromise(myNum));
console.log(isPromise(myString));

Output:

true
false
false

We see the results return as expected because when we test a promise, it returns true. And with two variables, number and string, the result is false.

Summary

This article showed you some ways to check if a value is a promise using JavaScript. You can get the code to test or follow to understand more about the above methods. I hope this article helps you and your program. Good luck.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *