Exactly, what Kevin has said.
Replace the whole callback (stuff inside .then() method) with something lke
this:
app.controller('MyCtrl', function() {
$http.get('/data/url)
.then(someObject);
// and later:
var data = [1, 2, 3];
}
So, what is going on here:
1. http.get gets called.
2. Whatever we get from get(), we call its' then() method. We give it
''some object'.
3. Data gets assigned. (var data =...)
Now, your $http.get has returned a promise. We have given that promise a
'callback'. So stuff like:
var myFun = function(response) {/*do stuff*/}
http.get().then(myFun);
is almost the same as
http.get().then(function (response) {/*do stuff */});
In both cases, you're not calling myFun. You're calling .then() and giving
it myFun as parameter.
So a long long time past that (probably a couple hundred thousand
nanoseconds :) the get() will receive the response, let's say it calls it
*response*. When it does, it has your myFun to call with the response. So
it will do that: myFun(response).
So order of operation:
1. http.get gets called.
2. Whatever we get from get(), we call its' then() method. We give it
''some object'.
3. Data gets assigned. (var data =...)
4. myFun(response)
Better?
On Saturday, April 26, 2014 5:28:17 AM UTC+2, Kevin Ingwersen wrote:
>
> Because the code with B) is a callback. Its first added to the stack of
> execution and ran later, which is why variables go out of scope in these
> cases. So the code
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.