DEV Community

Discussion on: What is a promise in javascript ? how does it solve callback hell issue?

Collapse
 
guyroyse profile image
Guy Royse

A promise in JavaScript is an abstraction around the idea of an asynchronous callback that supports chaining. This makes the code read to the humans like it is procedural. Do step #1 then step #2 then step #3 all while allowing the computer to handle all the asynchronicity in the all the calls.

It turns this code...

doAsyncThing(input, function(result) {
  doAnotherAysncThingUsingResult(result, function(anotherResult) {
    doFinalAsyncThing(anotherResult, function(finalResult) {
      console.log("It is finished: ", finalResult);
    });
  });
});

...into this code.

doAsyncThing(input)
  .then(result => doAnotherAysncThingUsingResult(result))
  .then(anotherResult => doFinalAsyncThing(anotherResult))
  .then(finalResult => console.log("It is finished: "", finalResult));

I blogged about this a few months ago on my company's blog here.

Collapse
 
dineshbabu153 profile image
Dineshbabu Thoota

Thank you Guy !! that was a neat and simple explanation.