-1

I am new to Javascript & AngularJS. I have a scenario as below:

$http.get('URL').success(function(data){

$scope.data = data;

});

$.fullCalender({
calendarData: $scope.data
});

In above code, I get blank for 'calendarData'

But I can resolve above issue as below:

$http.get('URL').success(function(data){
    $.fullCalender({
        calendarData: data
    });
}); 

So, my doubt is: When we can resolve issue as above, why people go for promises. Sorry if its a dumb query.

John
  • 55
  • 1
  • 12
  • promises are a tool, just like callbacks are a tool - use the right tool for the job - there is no concept of one being better than the other (one uses a hammer for nails, and a screwdriver for screws) - promises have the advantage of easy chaining, as opposed to the callback pyramid from hell scenario when "chaining" multiple asynchronous operations using callbacks – Jaromanda X Apr 27 '17 at 02:02
  • http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – epascarello Apr 27 '17 at 02:03
  • @JaromandaX Thanks a lot :) Sorry unable to upvote as I dont have enough points. – John Apr 27 '17 at 03:07
  • @epascarello Thanks for the link. – John Apr 27 '17 at 03:07

1 Answers1

0

Promises solve the famous callback hell issue. Callback hell really refers to 2 issues. The first is the idea that with callbacks you may sometimes need to nest callbacks and as such have very difficult to read code. Promises on the other hand can be chained which makes for much cleaner code.

Another issue is something called inversion of control. This is the big issue. The idea that in some cases the callback being called by some third party can be a scary thought. If for whatever reason there is some bug in said party , this bug will now leak in to your code.

An example would be if for whatever your reason your callback was getting invoked more than once. This can obviously be a fatal flaw. promises only execute the callback function once no matter what. In other words, reverting the inversion of control is a huge win with promises.

Chaim Friedman
  • 6,124
  • 4
  • 33
  • 61