解决Muliple Promise Node-JS

时间:2016-01-27 15:34:38

标签: angularjs node.js promise q

我有许多承诺。

applicationWillTerminate

我想处理所有承诺,无论是失败还是解决。

1 个答案:

答案 0 :(得分:0)

使用案例1:

您希望处理错误以便单独处理并在所有未调用上成功:



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);




用例2:

您希望处理要单独处理的错误并在所有上调用成功:



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);