Here's a convenient shortcut: Just call $q.when() with no arguments, then place all of your synchronous code (that you want to behave asynchronously) in a subsequent .then() call. This will automatically pick up any results, or exceptions, and pass them on as promises.
E.g.
function myAsyncSqrt(value) { return $q.when() .then(function() { return Math.sqrt(value); }); } ... var promise = myAsyncSqrt(16); promise.then( function(res) { alert('answer:' + res); }, function(err) { alert('uh oh:' + err); });
No comments:
Post a Comment