You need to return a promise from the function that is resolved or rejected when the $http
POST request is finished.
It looks like instead you're trying to return the reject
and resolve
products from the $http
function itself, while your saveTags
function ends up returning nothing to its caller (ie. from your forEach
loop).
Try this:
this.saveTags = function (tag) {
var deferred = $q.defer();
$http({
method: "POST",
url: '/api/projects/' + data.Id + '/tags',
data: ({ Name: tag.Name })
}).then(function (response) {
console.log(response.data)
if (typeof response.data === 'object') {
deferred.resolve(response.data);
} else {
// invalid response
deferred.reject(response.data)
}
}, function (response) {
// something went wrong
deferred.reject(response.data)
});
return deferred.promise;
}