Promises from a service into a controller - angularjs

This is probably down to me not fully understanding promises in angular. I just can't see where.
I have this service:
getSingleEventById : function(eventId)
{
var deferred = $q.defer();
$localForage.getItem('PublicEvents').then(function(results){
var found = $filter('filter')(results, {eventId: eventId}, true);
if (found.length)
{
deferred.resolve({'event':found[0]});
return deferred.promise;
} else {
return deferred.reject('not found');
}
});
},
being called from a controller:
$scope.event = myService.getSingleEventById($stateParams.eventId).then(function(foundEvent){
return foundEvent;
})
What i am seeing in the output is:
TypeError: Cannot read property 'then' of undefined
Where am i going wrong with my promise in my service?

You just need make getSingleEventById method return Promise object. So simply return result of then call.
Also you can improve your code by getting rid of redundant deferred object. You don't need it because whatever you return from then callbacks becomes new Promise object, which is passed into next then in queue.
getSingleEventById: function (eventId) {
return $localForage.getItem('PublicEvents').then(function (results) {
var found = $filter('filter')(results, {eventId: eventId}, true);
if (found.length) {
return {event: found[0]};
}
return $q.reject('not found');
});
},
Note how you return return {event: found[0]}; object. This is equivalent to previous deferred.resolve({'event':found[0]}); however without creating one more intermediate deferred object.

getSingleEventById : function(eventId)
{
var deferred = $q.defer();
$localForage.getItem('PublicEvents').then(function(results){
var found = $filter('filter')(results, {eventId: eventId}, true);
if (found.length)
{
deferred.resolve({'event':found[0]});
} else {
deferred.reject('not found');
}
});
return deferred.promise; // return the promise here
}

Related

How to get .notify() in chaining promises from $q.all?

I am using chained promises in angular js with $q service and it's working fine except the information of progressCallback ? let me draw what I have done so far?
calling function from my controller in below chainable promise way
fun1()
.then(resp1){
return fun2(resp1.id);
})
.then(resp2){
return $q.all([fun3(resp2.id),fun4(resp2.name)]);
})
.then(function(resp34){
return fun5();
})
.then(success)
.catch(errorhandler)
.finally(final);
and here is my all functions signature in service
var funX = function(param) {
var d = $q.defer();
d.notify('start with funX'); // Note: it was not working so placed inside else
doSomethingASync(param, function(err,data) {
if(err) { d.reject(err);}
else { d.notify('done with funX'); d.resolve(data); }
});
return d.promise;
});
Now my question is where do I receive this d.notify() message in my controller?
trial 1
.then(resp1, info1){
return fun2(resp1.id);
});
but it's undefined
trial 2
.then(resp1, err1, info1) {
return fun2(resp1.id);
}
but still undefined?
UPDATE
I have find a way by adding second parameter in finally()
.then().catch().finally(final, notify);
and here is my function definitions.
var errorHandler = function(err) {
console.error('Error returned from function:', err);
};
var final = function() {
console.log('Called Finally');
};
var notify = function(notification) {
console.log('Notify', notification);
};
var success = function(data) {
console.log('Success data');
console.log(data);
};
Can we get each promise function notification or this is not feasible?
But Now my query changed to
How do we add a .notify for the $q.all() ?
as I understand that $q.all returns a single promise which contains all promise resolve data;

Why won't promise resolve in a service?

I have a factory NewEditSvc where I'm trying to share functionality common to NewCtrl and EditCtrl. One thing I'm trying to do is get feeParameters from the server inside the service but this is not working.
angular.module('feeSuitesApp')
.factory('NewEditSvc', function($q, FeeTypesSvc){
var _feeParameters = {};
function getParameters(){
return _feeParameters;
}
function setParameters(feeType){
var promise = $q(function(resolve, reject){
FeeTypesSvc.resource.get({id: feeType.id, association: 'fee_parameters'}).
$promise.then(function(data){
resolve(data);
}, function(error){
reject(error);
})
});
promise.then(function(data){
_feeParameters = data.master;
});
}
return {
getParameters: getParameters,
setParameters: setParameters
}
});
angular.module('feeSuitesApp')
.controller('NewCtrl', function(NewEditSvc, feeType){
$scope.feeParameters = NewEditSvc.getParameters();
$scope.feeType = feeType;
$scope.setParameters = function(feeType){
NewEditSvc.setParameters(feeType);
};
$scope.setParameters($scope.feeType);
});
After my last line $scope.feeParameters is not set. But if I return the promise variable from NewEditSvc.setParameters() and then in my NewCtrl I do the following:
var promise = $scope.setParameters($scope.feeType);
promise.then(function(data){
$scope.feeParameters = data.master;
});
Then everything works. What am I doing wrong?
The problem is, that you're not returning your promise in setParameters. https://docs.angularjs.org/api/ng/service/$q/
Just add return promise.

Angular TypeError: Cannot read property 'then' of undefined

I have a data service like this:
this.myFunction= function(callback) {
var url = rootURL + "path1/path2/service.json";
var promise = $http.get(url);
promise.then(function(payload){
return callback(payload);
});
return promise;
}
It is called in a controller to initialize some stuff:
DataService.myFunction(function(data) {
if(data.statusText !== "OK"){
$scope.$worked= false;
}else{
$scope.$worked= true;
}
}
And I get "TypeError: Cannot read property 'then' of undefined". Console.log(data) in the callback shows a 200 "OK" response and the data I expect. I have searched for this error already and mostly it is due to not returning the promise in the service. However, I'm returning the promise. Setting anything on the controller scope in the callback causes the error.
Angular version: AngularJS v1.3.0-rc.2
Thanks!
You don't need to return a promise in this case, because you are using a callback. Callbacks and promises are the two ends of the spectrum. You can accomplish what you want simply with this.
If you want to use a callback you can leave your controller code.
this.myFunction= function(callback) {
var url = rootURL + "path1/path2/service.json";
$http.get(url).then(function(response) {
callback(response.data);
});
}
Or if you want to utilize the promises
this.myFunction= function() {
var url = rootURL + "path1/path2/service.json";
return $http.get(url).then(function(response) {
return response.data;
});
}
DataService.myFunction().then(function(data) {
if(data.statusText !== "OK"){
$scope.$worked = false;
} else {
$scope.$worked = true;
}
});

How to use Backbone.Model.save() promise with validation

I'm trying to use the promise returned from Backbone.model.save(). Actually, per spec, it returns a promise if valid and false if not. I'd like to use the return value, regardless of the type, in future deferred.done() and deferred.fail() calls. Like this:
var promise = model.save();
$.when(promise).done(function() {
console.log('success!');
});
$.when(promise).fail(function() {
console.log('dang');
});
But, $.when() when passed a non-promise fires, done(), so, in the above, if the model is invalid, $.when(false).done() fires, and you get "success!".
I know I can use the success and error attributes in save(), but with my code, it's advantageous to have multiple done() functions applied later. That's the power of the promise afterall.
So, I'm left with:
var promise = model.save();
if (promise) {
$.when(promise).done(function() {
console.log('success!');
});
$.when(promise).fail(function() {
console.log('dang');
});
} else {
console.log('dang');
}
I hate not being DRY.
var promise = model.save();
var fail = function() {
console.log('dang');
};
if (promise) {
$.when(promise).done(function() {
console.log('success!');
});
$.when(promise).fail(function() {
fail();
});
} else {
fail();
}
Geting pretty messy. You get the picture. I'm hoping I'm just missing something here.
You can override Backbone.save method to have your desired behavior. If the returned value from the original save function is a boolean (which means validation failed), just return a custom promise and reject its related deferred.
var oldSaveFunction = Backbone.Model.prototype.save;
Backbone.Model.prototype.save = function(){
var returnedValue = oldSaveFunction.apply(this, arguments),
deferred = new $.Deferred();
if(_.isBoolean(returnedValue)){
deferred.reject();
return deferred.promise();
}
return returnedValue;
}
var Person = Backbone.Model.extend({
url : 'http://www.google.com',
validate : function(attributes){
if(!("name" in attributes))
return "invalid";
}
});
var person = new Person();
$.when(person.save()).fail(function(){
console.log("failed");
});
Try it on this fiddle
http://jsfiddle.net/WNHXz/1/
Here's an improvement on a previous answer, but with better support for error handling. I needed it to avoid swallowing errors so here's what I did:
var oldSaveFunction = Backbone.Model.prototype.save;
Backbone.Model.prototype.save = function () {
var returnedValue = oldSaveFunction.apply(this, arguments),
fulfiller,
rejecter,
pendingPromise = new Promise(function (fulfill, reject) {
fulfiller = fulfill;
rejecter = reject;
});
if (_.isBoolean(returnedValue)) {
rejecter(this.validationError);
} else {
// Assuming returnedValue is a deferred
returnedValue.then(function success() {
fulfiller.apply(this, arguments);
}, function failure(jqxhr, errorName, error) {
rejecter(error);
});
}
return pendingPromise;
};
Hope it helps!

AngularJS: $q wait for all even when 1 rejected

I've been trying to wait for a couple of promises with Angular's $q but there seems to be no option to 'wait for all even when a promis is rejected'.
I've created an example (http://jsfiddle.net/Zenuka/pHEf9/21/) and I want a function to be executed when all promises are resolved/rejected, is that possible?
Something like:
$q.whenAllComplete(promises, function() {....})
EDIT: In the example you see that the second service fails and immediately after that the function in $q.all().then(..., function(){...}) is being executed. I want to wait for the fifth promise to be completed.
Ok, I've implemeted a basic version myself (I only want to wait for an array of promises). Anyone can extend this or create a cleaner version if they want to :-)
Check the jsfiddle to see it in action: http://jsfiddle.net/Zenuka/pHEf9/
angular.module('test').config(['$provide', function ($provide) {
$provide.decorator('$q', ['$delegate', function ($delegate) {
var $q = $delegate;
// Extention for q
$q.allSettled = $q.allSettled || function (promises) {
var deferred = $q.defer();
if (angular.isArray(promises)) {
var states = [];
var results = [];
var didAPromiseFail = false;
if (promises.length === 0) {
deferred.resolve(results);
return deferred.promise;
}
// First create an array for all promises with their state
angular.forEach(promises, function (promise, key) {
states[key] = false;
});
// Helper to check if all states are finished
var checkStates = function (states, results, deferred, failed) {
var allFinished = true;
angular.forEach(states, function (state, key) {
if (!state) {
allFinished = false;
}
});
if (allFinished) {
if (failed) {
deferred.reject(results);
} else {
deferred.resolve(results);
}
}
}
// Loop through the promises
// a second loop to be sure that checkStates is called when all states are set to false first
angular.forEach(promises, function (promise, key) {
$q.when(promise).then(function (result) {
states[key] = true;
results[key] = result;
checkStates(states, results, deferred, didAPromiseFail);
}, function (reason) {
states[key] = true;
results[key] = reason;
didAPromiseFail = true;
checkStates(states, results, deferred, didAPromiseFail);
});
});
} else {
throw 'allSettled can only handle an array of promises (for now)';
}
return deferred.promise;
};
return $q;
}]);
}]);
Analogous to how all() returns an array/hash of the resolved values, the allSettled() function from Kris Kowal's Q returns a collection of objects that look either like:
{ state: 'fulfilled', value: <resolved value> }
or:
{ state: 'rejected', reason: <rejection error> }
As this behavior is rather handy, I've ported the function to Angular.js's $q:
angular.module('your-module').config(['$provide', function ($provide) {
$provide.decorator('$q', ['$delegate', function ($delegate) {
var $q = $delegate;
$q.allSettled = $q.allSettled || function allSettled(promises) {
// Implementation of allSettled function from Kris Kowal's Q:
// https://github.com/kriskowal/q/wiki/API-Reference#promiseallsettled
var wrapped = angular.isArray(promises) ? [] : {};
angular.forEach(promises, function(promise, key) {
if (!wrapped.hasOwnProperty(key)) {
wrapped[key] = wrap(promise);
}
});
return $q.all(wrapped);
function wrap(promise) {
return $q.when(promise)
.then(function (value) {
return { state: 'fulfilled', value: value };
}, function (reason) {
return { state: 'rejected', reason: reason };
});
}
};
return $q;
}]);
}]);
Credit goes to:
Zenuka for the decorator code
Benjamin Gruenbaum for pointing me in the right direction
The all implementation from Angular.js source
The promise API in angularJS is based on https://github.com/kriskowal/q. I looked at API that Q provides and it had a method allSettled, but this method has not been exposed over the port that AngularJS uses. This is form the documentation
The all function returns a promise for an array of values. When this
promise is fulfilled, the array contains the fulfillment values of the
original promises, in the same order as those promises. If one of the
given promises is rejected, the returned promise is immediately
rejected, not waiting for the rest of the batch. If you want to wait
for all of the promises to either be fulfilled or rejected, you can
use allSettled.
I solved this same issue recently. This was the problem:
I had an array of promises to handle, promises
I wanted to get all the results, resolve or reject
I wanted the promises to run in parallel
This was how I solved the problem:
promises = promises.map(
promise => promise.catch(() => null)
);
$q.all(promises, results => {
// code to handle results
});
It's not a general fix, but it is simple and and easy to follow. Of course if any of your promises could resolve to null then you can't distinguish between that a rejection, but it works in many cases and you can always modify the catch function to work with the particular problem you're solving.
Thanks for the inspiration Zenuka, you can find my version at https://gist.github.com/JGarrido/8100714
Here it is, in it's current state:
.config( function($provide) {
$provide.decorator("$q", ["$delegate", function($delegate) {
var $q = $delegate;
$q.allComplete = function(promises) {
if(!angular.isArray(promises)) {
throw Error("$q.allComplete only accepts an array.");
}
var deferred = $q.defer();
var passed = 0;
var failed = 0;
var responses = [];
angular.forEach(promises, function(promise, index) {
promise
.then( function(result) {
console.info('done', result);
passed++;
responses.push(result);
})
.catch( function(result) {
console.error('err', result);
failed++;
responses.push(result);
})
.finally( function() {
if((passed + failed) == promises.length) {
console.log("COMPLETE: " + "passed = " + passed + ", failed = " + failed);
if(failed > 0) {
deferred.reject(responses);
} else {
deferred.resolve(responses);
}
}
})
;
});
return deferred.promise;
};
return $q;
}]);
})
A simpler approach to solving this problem.
$provide.decorator('$q', ['$delegate', function ($delegate) {
var $q = $delegate;
$q.allSettled = $q.allSettled || function (promises) {
var toSettle = [];
if (angular.isArray(promises)) {
angular.forEach(promises, function (promise, key) {
var dfd = $q.defer();
promise.then(dfd.resolve, dfd.resolve);
toSettle.push(dfd.promise);
});
}
return $q.all(toSettle);
};
return $q;
}]);
A simple solution would be to use catch() to handle any errors and stop rejections from propagating. You could do this by either not returning a value from catch() or by resolving using the error response and then handling errors in all(). This way $q.all() will always be executed. I've updated the fiddle with a very simple example: http://jsfiddle.net/pHEf9/125/
...
function handleError(response) {
console.log('Handle error');
}
// Create 5 promises
var promises = [];
var names = [];
for (var i = 1; i <= 5; i++) {
var willSucceed = true;
if (i == 2) willSucceed = false;
promises.push(
createPromise('Promise' + i, i, willSucceed).catch(handleError));
}
...
Be aware that if you don't return a value from within catch(), the array of resolved promises passed to all() will contain undefined for those errored elements.
just use finally
$q.all(tasks).finally(function() {
// do stuff
});

Resources