javascript - Angular problems chaining $resource promises -
i trying chain promises service have $stateprovider resolves without waiting $q. results in controller getting result object empty array, , object gets filled later on. promise chain going wrong?
my $stateprovider:
.state('somestate', { resolve: { neededvalue: function(anotherservice) { return anotherservice.getdata(); //this 1 works pretty , doesn't use chaining }, finalvalue: function(myservice, neededvalue) { return myservice.startchain(neededvalue); } } }
my service:
app.factory("myservice", ['$resource', '$q', function ($resource, $q) { var firstapi = $resource("api/somewhere/first", {}, { "getdata": { method: "post" } }); var secondapi = $resource("api/somewhere/second", {}, { "getdata": { method: "post" } }); var result = { array: [] }; var myservice = { startchain: startchain }; return myservice; function startchain(someneededvalues) { var deferred = $q.defer(); firstapi .getdata(someneededvalues.values).$promise .then(function (data) { result.value = data.value; }) .then(recurringaction) .then(deferred.resolve(result)); return deferred.promise; } function recurringaction() { return secondapi.getdata(result.value, function (data) { angular.copy(data.array, result.array); }).$promise; } }]);
not sure if script worked way intended when passed deferred.resolve(result)
callback promise...
so change
.then(deferred.resolve(result));
to
.then(function(){ deferred.resolve(result) });
Comments
Post a Comment