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

Did you find this post useful? Show some love!

It's funny you should ask, I was just reading the MDN Docs about this yesterday. They explain the whole concept of Promises really well. Their explanation of what promises give you vs. callbacks is a good one:

Unlike old-style passed-in callbacks, a promise comes with some guarantees:
• Callbacks will never be called before the completion of the current run of the JavaScript event loop.
• Callbacks added with .then even after the success or failure of the asynchronous operation, will be called, as above.
• Multiple callbacks may be added by calling .then several times, to be executed independently in insertion order.

But the most immediate benefit of promises is chaining.

Basically promises are much more composable + predictable than callbacks, but I heartily recommend reading through the whole article to get a better overall idea.

Thank you Donald.i am going through the MDN article.

One of the best explanations I read was this: qntm.org/files/promise/promise.html
It is a little longer but covers the topic well and in a very straightforward way with plenty of examples, I would recommend it

Ben Halpern DEV.TO FOUNDER

Hey there, we see you aren't signed in. (Yes you, the reader. This is a fake comment.)

Please consider creating an account on dev.to. It literally takes a few seconds and we'd appreciate the support so much. ❤️

Plus, no fake comments when you're signed in. 🙃

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.

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

This post: scotthannen.org/blog/2016/03/01/ch...
Shows a side-by-side (or above-and-below) example of the same code with callbacks and with promises. It was a few years ago. I probably wouldn't cram all those promises into one line of code like that. But even still it shows the simplification.

Classic DEV Post from Apr 20

How do you avoid feelings of entitlement?

At a certain level of achievement in a software, it can be easy to become entit...

READ POST
Follow @ben to see more of their posts in your feed.
Dineshbabu Thoota
Member since Mar 7, 2018
Trending on dev.to
JavaScript Universal Windows Applications
#javascript #vue #aurelia #angular
Here's Why Mapping a Constructed Array in JavaScript Doesn't Work
#softwareengineering #javascript #programming #softwaredevelopment
Introducing JavaScript Objects
#beginners #javascript #webdev
Node.js Top 10 Articles — June 2018
#node #javascript #webdev #tech
Oh Javascript... 🙄
#javascript #badparts
Sails.js is a Node.js MVC framework inspired by Ruby on Rails
#mvc #webdev #javascript #node
JavaScript: Equality insanity, or where x === 1 && x === 2
#javascript #challenge
What is GraphQL
#graphql #javascript #facebook #node