angularjs的非同步處理機制
阿新 • • 發佈:2019-01-10
function q1() { var deferred = $q.defer(); $timeout(function () { deferred.notify("1 notify"); if (1==1) { deferred.resolve("1 resolved"); } else { deferred.reject("1 reject"); } }, 1000); return deferred.promise; } function q2() { var deferred = $q.defer(); $timeout(function () { deferred.notify("2 notify"); if (1!=1) { deferred.resolve("2 resolved"); } else { deferred.reject("2 reject"); } }, 1000); return deferred.promise; } function q3() { var deferred = $q.defer(); $timeout(function () { deferred.notify("3 notify"); if (1!=1) { deferred.resolve("3 resolved"); } else { deferred.reject("3 reject"); } }, 1000); return deferred.promise; } function q4() { var deferred = $q.defer(); $timeout(function () { deferred.notify("4 notify"); if (1==1) { deferred.resolve("4 resolved"); } else { deferred.reject("4 reject"); } }, 1000); return deferred.promise; }
在上面,我定義了四個promise,其中有兩個是reject的,那麼我們來看看輸出結果是怎麼樣的呢?