* Constructs a promise manager. * * @param {function(function)} nextTick Function for executing functions in the next turn. * @param {function(...*)} exceptionHandler Function into which unexpected exceptions are passed for * debugging purposes. * @param {boolean=} errorOnUnhandledRejection
(nextTick, exceptionHandler, errorOnUnhandledRejections)
| 17874 | * @returns {object} Promise manager. |
| 17875 | */ |
| 17876 | function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) { |
| 17877 | var $qMinErr = minErr('$q', TypeError); |
| 17878 | var queueSize = 0; |
| 17879 | var checkQueue = []; |
| 17880 | |
| 17881 | /** |
| 17882 | * @ngdoc method |
| 17883 | * @name ng.$q#defer |
| 17884 | * @kind function |
| 17885 | * |
| 17886 | * @description |
| 17887 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 17888 | * |
| 17889 | * @returns {Deferred} Returns a new instance of deferred. |
| 17890 | */ |
| 17891 | function defer() { |
| 17892 | return new Deferred(); |
| 17893 | } |
| 17894 | |
| 17895 | function Deferred() { |
| 17896 | var promise = this.promise = new Promise(); |
| 17897 | //Non prototype methods necessary to support unbound execution :/ |
| 17898 | this.resolve = function(val) { resolvePromise(promise, val); }; |
| 17899 | this.reject = function(reason) { rejectPromise(promise, reason); }; |
| 17900 | this.notify = function(progress) { notifyPromise(promise, progress); }; |
| 17901 | } |
| 17902 | |
| 17903 | |
| 17904 | function Promise() { |
| 17905 | this.$$state = { status: 0 }; |
| 17906 | } |
| 17907 | |
| 17908 | extend(Promise.prototype, { |
| 17909 | then: function(onFulfilled, onRejected, progressBack) { |
| 17910 | if (isUndefined(onFulfilled) && isUndefined(onRejected) && isUndefined(progressBack)) { |
| 17911 | return this; |
| 17912 | } |
| 17913 | var result = new Promise(); |
| 17914 | |
| 17915 | this.$$state.pending = this.$$state.pending || []; |
| 17916 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 17917 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 17918 | |
| 17919 | return result; |
| 17920 | }, |
| 17921 | |
| 17922 | 'catch': function(callback) { |
| 17923 | return this.then(null, callback); |
| 17924 | }, |
| 17925 | |
| 17926 | 'finally': function(callback, progressBack) { |
| 17927 | return this.then(function(value) { |
| 17928 | return handleCallback(value, resolve, callback); |
| 17929 | }, function(error) { |
| 17930 | return handleCallback(error, reject, callback); |
| 17931 | }, progressBack); |
| 17932 | } |
| 17933 | }); |
no test coverage detected