DEV Community

Ajay Baraiya
Ajay Baraiya

Posted on • Edited on

Promise in Javascript #Node Quick Notes.

Take it easy as a javascript if else statement that we use every time whether a condition is true or not.

If(we got success in something){
return resolution of successs;
}else{
return rejection;
}

As above same a Promise can be defined but remember to use new key word while defining it.

const myPromise = new Promise(
(success,failure) => { if(true){success("resolve");} else {failure("reject");};}
);

const functionToExecutePromise = () => {
  myPromise
    .then(ok => {
      console.log(ok)
    })
    .catch(err => {
      console.error(err)
    })
}

functionToExecutePromise();

Enter fullscreen mode Exit fullscreen mode

Top comments (0)