angularjs - Resolving Muliple Promise Node-JS -
i have array of promises.
var defered = q.defer(); // promises array have different promises var promises = []; q.all(promises).then(function(response){ // susseccfully resolved promise execution come here }, function(error){ // if promise failed execution come here });
i want process promises whether fail or resolved.
use case 1:
you want handle errors handled separately , success on not called:
var myapp = angular.module('myapp', []); function myctrl($scope, $q, $timeout) { function prone(){ var d = $q.defer(); settimeout(function(){ d.reject(); }, 100); return d.promise; } function prtwo(){ var d = $q.defer(); settimeout(function(){ d.resolve(); }, 100); return d.promise; } $q.all([prone().catch(function(){ console.log('fail one'); return $q.reject() }), prtwo()]).then(function(){ console.log('success'); return $q.resolve(); }).catch(function(){ console.log('error'); });; } myapp.controller('myctrl', myctrl);
use case 2:
you want handle errors handled separately , success on called anyway:
var myapp = angular.module('myapp', []); function myctrl($scope, $q, $timeout) { function prone(){ var d = $q.defer(); settimeout(function(){ d.reject(); }, 100); return d.promise; } function prtwo(){ var d = $q.defer(); settimeout(function(){ d.resolve(); }, 100); return d.promise; } $q.all([prone().catch(function(){ console.log('fail one'); return $q.resolve() }), prtwo()]).then(function(){ console.log('success'); return $q.resolve(); }).catch(function(){ console.log('error'); });; } myapp.controller('myctrl', myctrl);
Comments
Post a Comment