$q.when
$q.when() can be used to wrap an object or function if we are not sure whether it returns a promise or not. If we wrap a value object or a value returned function, the value will be passed as a result argument to the next function. When we wrap a function, the when() method will ensure that the function is executed first before going to the next one.
Below are some code examples:
$q.when('aaa').then(function(result) { alert(result); })
var vm = this; vm.chars = ''; $q.when(_test()).then(function(){ vm.chars = vm.chars + 'END' }) _test = function() { for (i = 0; i <= 10000; i++) { vm.chars = vm.chars + '.' } }
$q.all
$q.all() is used to combine promise returned functions and only continue to progress further after all of the functions complete successfully. If one of the functions fails, immediately as soon as $q.all() gets a rejected promise from one of the functions, it will end the execution and pass forward to an error callback if there is any.
vm.numbers = ''; _promiseFunction = function(x) { var deferred = $q.defer(); if (x > 0) { deferred.resolve(x * 100); } else { deferred.reject(x + " is smaller than 0") } return deferred.promise; } var first = _promiseFunction(1); var second = _promiseFunction(5); var third = _promiseFunction(10); var four = _promiseFunction(-1); var five = _promiseFunction(-10) $q.all([first, second, third /*, four, five*/]). then(function(results){ console.log(results.length); for(i=0; i < results.length; i++) { vm.numbers = vm.numbers + ' ' + results[i]; } }, function(errors){ // if there is any error in the sequence, the success callback will not be called console.log(errors); })
$q.then
$q.then() is useful for executing asynchronous functions (or blocks of codes) sequentially in a chain. If the function inside a then() does not return a promise, it will be wrapped as a resolved promise. Otherwise the resolved or rejected promise will be passed forward.
_promiseFunction(5).then( function() { alert('aaa')} ).then( function() { alert('bbb');} ).then( function() {alert('ccc');} );
_promiseFunction(5).then( function() { return $q.reject('failed')} ).then( function(){ alert('result success')}, function(){ alert('result fail')} )
Note also that if we return a non-promise value in the error callback inside then(), this will be treated as a resolved promise. So we need to be careful when handling error if our intention is to pass the error forward to the next function.
$q.reject().then( // success function() {}, // error function(error) { return 'i got the error' } ).then( //success function() { alert('success'); }, // error function() { alert('error') } )
$q.reject
Last thing, we have $q.reject() that is useful as a quick way to create then send a rejected promise (with a value optionally) to the next function in a chain. For example, in a chain of multiple then(). We can see this in the last two examples.
See the examples in Plunker.
No comments:
Post a Comment