1

I am bit confused between success call back in Ajax and Promise.

Let me try to explain my confusion with an example.

$http.get("https://someapi.com").success(function (){})

In the above code, success it our callback function which is going to be executed whenever the asynchronous operation completes .. right?

Now coming to Promise.

We create a promise to handle both the Success and Failure (Resolve and Reject)

Now my question, when we have call back provision in Ajax, why do we need promise?

Jasen
  • 14,030
  • 3
  • 51
  • 68
Murali V
  • 51
  • 1
  • 3
  • 9
  • How about looking at https://www.quora.com/Whats-the-difference-between-a-promise-and-a-callback-in-Javascript ? – Beomi Oct 04 '17 at 04:12
  • 3
    Possible duplicate of [Aren't promises just callbacks?](https://stackoverflow.com/questions/22539815/arent-promises-just-callbacks) – lztachyon Oct 04 '17 at 04:14

2 Answers2

1

Promises allow to be able to use the result of the callback and/or chain several calls together to manipulate the result. With the introduction of async/await, it can make asynchronous code a lot simpler.

With callbacks it can be done, but with lots of nesting and potential confusion.

With Callbacks:

 $http.get('https://something.com/rest/returns5').success(function(result) {
    if (result) {
       var finalValue = result - 4;
       // to use the result you'll need another nested callback here
       callback(finalValue); // what scope did this come from? where did this come from? :-)
    }
 });

With Promises:

 $http.get('https://something.com/rest/returns5')
      .then(function(result) {
          return result - 4;
      })
      .then(function(result) {
          // use the result from the previous chained promise call (result - 4) 
      });
Dan D
  • 2,493
  • 15
  • 23
1

The benefit of using promises is just that they provide a powerful, common interface for async tasks.

The main benefit of a common interface (any common interface) is that you can write and use higher-level functions that operate on promises. As just one example, many promise libraries provide a function, often called all, that takes a set of promises, and returns a new promise that encapsulates all of them. Then you can write things like:

promise.all(promise1, promise2).then(function (value1, value2) {
    // blah blah
});

instead of this pyramid code

promise1.then(function (value1) {
    promise2.then(function (value2) {
        // blah blah
    });
});

And of course you can only use functions like all if you're using async tasks with some known, common interface. It would be impossible if some had .then methods, some had .success methods, and so on.

The promise interface has other useful properties, for instance the .then method on a promise returns a promise - and if the callback you pass it returns a promise, the .then method returns a promise that actually awaits the promise returned by the callback. In other words:

// p1 is a promise that resolves to 1, when somePromise resolves
var p1 = somePromise.then(function (value) {
    return 1;
});

// p2 is a promise that resolves to what someOtherPromise resolves to,
// not to someOtherPromise itself
var p2 = somePromise.then(function (value) {
    return someOtherPromise;
});

Which makes it nice and easy to make a series of HTTP calls, one after another.

There are some drawbacks to using promises too... By convention, they wrap your callback in a try/catch block, so if you throw an exception it will be caught, sometimes invisibly.

somePromise.then(function (value) {
    throw 'error';
}).catch(function (error) {
    // without this catch block, the error may be eaten invisibly
    console.error(error);
});

To sum up, a promise is just a widely-used interface for managing asynchronous tasks. Use it if you want, don't if you don't.

jcarpenter2
  • 5,312
  • 4
  • 22
  • 49