I am having a hard time using promises and passing information between a service and controller.
The code below functions, in that it makes the calls, I get the promises back, I perform work on them, etc. I want to pass status messages back to the controller now, but I can't figure out to return them to the controller.
How can I sync myService
's non-promise response with my controller variable? Or should I just be returning the $q.all
promise back to the controller and then having the controller call the statusService
?
Ideally, I want the controller to just "fire and forget" and wait for a status response so I can update a modal.
Controller
function doWork() {
myService.doSomeWork();
// what I'm going for:
// var statusMessage = myService.doSomeWork(sharedDataService.onThisThing);
}
Service
factory.doSomeWork = function() {
var promises = [
queryApi("firstApiCall"),
queryApi("secondApiCall")
];
$q.all(promises)
.then(function(res) {
workService.doSomething(res[0].data, res[1].data);
console.log("response:", res);
// I want to return a string, or object, back to a variable in my controller
//return statusService.getStatusMessage(res[0]);
}, function(err) {
console.log("error:", err);
//return statusService.getStatusMessage(err);
}
);
};
function queryApi(url) {
return $http.get(url);
}
Any help would be appreciated.