Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialkelvin atawura
Courses Plus Student 580 PointsdataService.getTodos() call
After watching the view l was sort of wondering a view thing. why was the service not called in this way
$scope.todos = dataService.getTodos(function(res) {
console.log(res.data);
$scope.todos = res.data;
});
However was called in this way instead
dataService.getTodos(function(res) {
console.log(res.data);
$scope.todos = res.data;
});
Both seems to do the same job and first one makes so much more sense to me. What's the pros and cons of using either of these?
Thomas Nilsen
14,957 PointsThomas Nilsen
14,957 PointsAlright - Let's talk about the first example
The only reason this works is because you write $scope.todos = res.data; inside the callback function as well.
If you were to write it like this:
It wouldn't work.
This function is asynchronous - meaning you won't get back a value right away. You have to get it inside the callback function.
This is an important aspect of javascript.
I would recommend reading this