Google Groups

Re: [angular.js] Re: chaining $http calls?


Pete Bacon Darwin May 5, 2012 8:59 AM
Posted in group: AngularJS
It is true that $defer is better for delays than setTimeout when in an angular app but I suspect that coli was just using it to simulate an asynch call to something outside angular in which case there would be no setTimeout but it appears that we still need the $apply.
By the way in your fiddle Thomas you put that you are delaying for 100ms to allow the scope change to come into affect.  This is not how angular works.  There is no other thread that will kick in and notice the scope change if you just wait for a bit.  What is actually happening is that by calling $defer(...,300) you are actually triggering a $apply, which in turn triggers a $digest which catches the scope changes.  This can be done simply with a $apply if you know you are outside angular and you don't need a delay.  If you are not sure if you are in an angular $apply call already then you can use $defer without a time-out value, which effectively triggers a new $apply loop immediately after this call has completed.
Pete

On 5 May 2012 15:14, ThomasBurleson <thomasb...@gmail.com> wrote:
Best not to hook into window.setTimeout(). 
Instead use AngularJS $defer() to pause/delay a function call.

Also, you should wait after $scope changes before you call alerts.




On Friday, May 4, 2012 12:33:59 PM UTC-5, coli wrote:
Interesting, I didn't realize you have to call $scope.$apply(), after promise.resolve() 

The example does have the apply call, but it should probably be called out that it is needed even if you did not change the scope model data.

On Friday, May 4, 2012 10:24:58 AM UTC-7, coli wrote:
This appears to work

$http().then(function(){
    return $http();
}.then(function(){
    return $http();
}.then(function(){
    //done
});


Doesn't work. the alert("done") was never called. Anyone know why?

On Friday, May 4, 2012 9:50:27 AM UTC-7, coli wrote:
Trying to get my head around how to chain two (or more)$http calls in serial? (without nested callbacks)

Something like?

$http().then(
    $http() ?
).then(
    $http() ?
)

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/-AosPj3-Y0EJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.