1. 程式人生 > >angularjs的非同步處理機制

angularjs的非同步處理機制

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的,那麼我們來看看輸出結果是怎麼樣的呢?