简体   繁体   中英

Resolving Muliple Promise Node-JS

I have array of promises.

var defered = q.defer();
// promises is a array which will have different promises
var promises = [];

q.all(promises).then(function(response){

// SUSSECCFULLY RESOLVED ALL PROMISE THEN EXECUTION COME HERE

}, function(error){

// IF ANY PROMISE FAILED THEN EXECUTION COME HERE

});

I want to process all promises whether they fail or they resolved.

Use case 1:

You want to handle errors to be handled separately and success on all 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 for 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 to handle errors to be handled separately and success on all 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 for one'); return $q.resolve() }), prTwo()]).then(function(){ console.log('success'); return $q.resolve(); }).catch(function(){ console.log('error'); });; } myApp.controller('MyCtrl', MyCtrl); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM