Thursday, October 26, 2017

What is Promise in NodeJs and example

NodeJs
Promises are a compelling alternative to callbacks when dealing with asynchronous code. Unfortunately, promises can be confusing and perhaps you’ve written them off. However, significant work has been done to bring out the essential beauty of promises in a way that is interoperable and verifiable.

You can make the function  that provide both a promise and callback interface. For example, let’s . how to deal with the real call back function to avoid call hell with Promise object.

var goal=false;
var youGol=new Promise(
function(resolve,reject ){

if(goal){
var myGoal={status:"Bikash is carrier Oriented"};
resolve(myGoal);
}else{
var reson=new Error('Bikash Nothing set his carrier');
reject(reson);
}

}
);

var goalstatu=function(){
youGol.then(function (answers){
console.log(answers);
}).catch(function (error){
console.log(error.message);
});

}



goalstatu();

No comments:

Post a Comment