function asyncFunction() { var deferred = $q.defer(); doSomethingAsync().then(function(res) { res = asyncManipulate(res); deferred.resolve(res); }, function(err) { deferred.reject(err); }); return deferred.promise; }
$q.defer()
is meaningless. The author of the code clearly did not know that then so would return the promise. To improve the code, simply return the result to then: function asyncFunction() { return doSomethingAsync().then(function(res) { return asyncManipulate(res); }); }
function asyncFunction() { return doSomethingAsync().then(function(res) { return asyncManipulate(res); }, function(err) { return $q.reject(err); }); }
doSomethingAsync
function, either resolve or reject, will “pop up” until it finds its handler (if the handler exists at all). This means that if there is no need to process the result, then you can simply omit the corresponding handler, because the result will not disappear anywhere, it will simply pass on. In this example, the second handler can be safely removed (reject processing), since no manipulations are performed: function asyncFunction() { return doSomethingAsync().then(function(res) { return asyncManipulate(res); }); }
function asyncFunction() { return doSomethingAsync().then(null, function(err) { return errorHandler(err); }); }
function asyncFunction() { return doSomethingAsync().catch(function(err) { return errorHandler(err); }); }
asyncFunction().then(function (res) { // some code return res; }, function (res) { // some code }).then(function (res) { console.log('in resolve'); }, function (res) { console.log('in reject'); });
asyncFunction
function asyncFunction
, we will see 'in resolve' in the console. This is because there is only one way to be in the reject handler - to return $ q.reject (). In any other cases, the resolve handler will be called. We will rewrite the code so that we can see in the asyncFunction
console in the console if asyncFunction
returns a reject: asyncFunction().then(function (res) { // some code return res; }, function (res) { // some code return $q.reject(res); }).then(function (res) { console.log('in resolve'); }, function (res) { console.log('in reject'); });
asyncFunction().then(function (res) { importantFunction(); return res; }, function (err) { importantFunction(); return $q.reject(err); }).then(function (res) { // some resolve code }, function (err) { // some reject code })
asyncFunction().finally(function () { importantFunction(); }).then(function (res) { // some resolve code }, function (err) { // some reject code })
loadSomeInfo().then(function(something) { loadAnotherInfo().then(function(another) { doSomethingOnThem(something, another); }); });
doSomethingOnThem
functions require the result of executing both loadSomeInfo
and loadAnotherInfo
. And it does not matter in what order they will be called, it is only important that the doSomethingOnThem
function be called after the result from both functions is obtained. This means that these functions can be called in parallel. But the author of this code clearly did not know about the $ q.all method. Rewrite: $q.all([loadSomeInfo(), loadAnotherInfo()]).then(function (results) { doSomethingOnThem(results[0], results[1]); });
results
, in which are the results of all functions respectively. var promise; if (isAsync){ promise = asyncFunction(); } else { var localPromise = $q.defer(); promise = localPromise.promise; localPromise.resolve(42); } promise.then(function (res) { // some code });
$q.when(isAsync? asyncFunction(): 42).then(function (res) { // some code });
function asyncFunction(){ return $timeout(function meAsynk(){ throw new Error('error in meAsynk'); }, 1); } try{ asyncFunction(); } catch(err){ errorHandler(err); }
asyncFunction
function. But, after $timeout
launches its callback function meAsynk
, any errors that occur there will fall into the application's uncaught exception handler. Accordingly, our catch handler will not know anything.catch
sugar described above): function asyncFunction(){ return $timeout(function meAsynk(){ throw new Error('error in meAsynk'); }, 1); } asyncFunction().catch(function (err) { errorHandler(err); });
function asyncFunction() { var promise = doSomethingAsync(); promise.then(function() { return somethingAsyncAgain(); }); return promise; }
somethingAsyncAgain
returns a reject (and as we already know, the reject is also called in cases where errors are falling), the code that caused our function will never know about it. Promises must be consistent, each following should depend on the previous one. But in this example, the promise is broken. To fix the rewrite so: function asyncFunction() { return doSomethingAsync().then(function() { return somethingAsyncAgain(); }); }
somethingAsyncAgain
, and all errors can be handled by the parent code. asyncFunction().then( function() { return somethingElseAsync(); }, function(err) { errorHandler(err); });
somethingElseAsync
function, it will not be handled by anyone. Let's rewrite the code so that the reject handler is detached: asyncFunction().then(function() { return somethingElseAsync(); }).catch(function(err) { errorHandler(err); });
Source: https://habr.com/ru/post/221111/
All Articles