* 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. * @returns {object} Promise manager.
(nextTick, exceptionHandler)
| 15042 | * @returns {object} Promise manager. |
| 15043 | */ |
| 15044 | function qFactory(nextTick, exceptionHandler) { |
| 15045 | var $qMinErr = minErr('$q', TypeError); |
| 15046 | function callOnce(self, resolveFn, rejectFn) { |
| 15047 | var called = false; |
| 15048 | function wrap(fn) { |
| 15049 | return function(value) { |
| 15050 | if (called) return; |
| 15051 | called = true; |
| 15052 | fn.call(self, value); |
| 15053 | }; |
| 15054 | } |
| 15055 | |
| 15056 | return [wrap(resolveFn), wrap(rejectFn)]; |
| 15057 | } |
| 15058 | |
| 15059 | /** |
| 15060 | * @ngdoc method |
| 15061 | * @name ng.$q#defer |
| 15062 | * @kind function |
| 15063 | * |
| 15064 | * @description |
| 15065 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 15066 | * |
| 15067 | * @returns {Deferred} Returns a new instance of deferred. |
| 15068 | */ |
| 15069 | var defer = function() { |
| 15070 | return new Deferred(); |
| 15071 | }; |
| 15072 | |
| 15073 | function Promise() { |
| 15074 | this.$$state = { status: 0 }; |
| 15075 | } |
| 15076 | |
| 15077 | extend(Promise.prototype, { |
| 15078 | then: function(onFulfilled, onRejected, progressBack) { |
| 15079 | if (isUndefined(onFulfilled) && isUndefined(onRejected) && isUndefined(progressBack)) { |
| 15080 | return this; |
| 15081 | } |
| 15082 | var result = new Deferred(); |
| 15083 | |
| 15084 | this.$$state.pending = this.$$state.pending || []; |
| 15085 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 15086 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 15087 | |
| 15088 | return result.promise; |
| 15089 | }, |
| 15090 | |
| 15091 | "catch": function(callback) { |
| 15092 | return this.then(null, callback); |
| 15093 | }, |
| 15094 | |
| 15095 | "finally": function(callback, progressBack) { |
| 15096 | return this.then(function(value) { |
| 15097 | return handleCallback(value, true, callback); |
| 15098 | }, function(error) { |
| 15099 | return handleCallback(error, false, callback); |
| 15100 | }, progressBack); |
| 15101 | } |
no test coverage detected