PROMISE

Question : Write a program that takes one parameter, if the parameter is equal to “iam_boolean” then it should resolve the promise else it should reject the promise.

Solution

function iam_promise(key) {
return new Promise((resolve, reject) => {
if (key === "iam_boolean") {
resolve("resolved");
} else {
reject("rejected");
}
});
}
iam_promise("iam_boolean")
.then((data) => console.log(data))
.catch((err) => console.log(err));
//Prints: resolved
iam_promise("Hello")
.then((data) => console.log(data))
.catch((err) => console.log(err));
//Prints: rejected