Here is the code where I am attempting to utilize promise
to store data from an asynchronous call in a variable, but it's not functioning as expected. I am relatively new to promises and after some research, I learned that promises can be helpful in such scenarios. However, I am struggling to implement it correctly. Could you please point out where I might be going wrong in the following code snippet?
angular.module("app").controller("myCtrl", function($scope, $http, $q) {
var deferred = $q.defer();
var data = $http.get("/api/events").success(function(response){
deferred.resolve(response);
return deferred.promise;
// I have also tried returning response directly;
})
console.log("DATA--");
console.log(data);
});
EDIT -
I am trying to make two API calls -
1) Form an array of IDs from the first API call.
2) Iterate through the array to make a second API call based on each ID.
3) Combine certain data from array 1 and array 2.
The specific use case I am attempting is described further in the following link, where I am exploring the use of promises to achieve this -