15
Apr

Using function call to determine the results of a Promise in javascript

Following code snippet uses a “Custom Javascript function” to invoke “resolve” / “reject” methods of a promise

describe(“Functions as Promise in JavaScript”, function(){

var promise = new Promise(function(resolve,reject){
if(myTest()){
resolve()
} else{
reject(new Error(“My Custom error message”))
}
})

function myTest(){
var res = false;
if(“a” === “b”)
{
res = true;
} else{
res = false;
}
return res;
}
test(“test”, ()=>{

promise.then(()=> console.log(“Success”))
.catch(err => console.log(“Failed with error”, err.message))

})
})

Output:
● Console

console.log my-test.js:24
Failed with error My Custom error message