* 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)
| 14665 | * @returns {object} Promise manager. |
| 14666 | */ |
| 14667 | function qFactory(nextTick, exceptionHandler) { |
| 14668 | var $qMinErr = minErr('$q', TypeError); |
| 14669 | function callOnce(self, resolveFn, rejectFn) { |
| 14670 | var called = false; |
| 14671 | function wrap(fn) { |
| 14672 | return function(value) { |
| 14673 | if (called) return; |
| 14674 | called = true; |
| 14675 | fn.call(self, value); |
| 14676 | }; |
| 14677 | } |
| 14678 | |
| 14679 | return [wrap(resolveFn), wrap(rejectFn)]; |
| 14680 | } |
| 14681 | |
| 14682 | /** |
| 14683 | * @ngdoc method |
| 14684 | * @name ng.$q#defer |
| 14685 | * @kind function |
| 14686 | * |
| 14687 | * @description |
| 14688 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 14689 | * |
| 14690 | * @returns {Deferred} Returns a new instance of deferred. |
| 14691 | */ |
| 14692 | var defer = function() { |
| 14693 | return new Deferred(); |
| 14694 | }; |
| 14695 | |
| 14696 | function Promise() { |
| 14697 | this.$$state = { status: 0 }; |
| 14698 | } |
| 14699 | |
| 14700 | extend(Promise.prototype, { |
| 14701 | then: function(onFulfilled, onRejected, progressBack) { |
| 14702 | if (isUndefined(onFulfilled) && isUndefined(onRejected) && isUndefined(progressBack)) { |
| 14703 | return this; |
| 14704 | } |
| 14705 | var result = new Deferred(); |
| 14706 | |
| 14707 | this.$$state.pending = this.$$state.pending || []; |
| 14708 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 14709 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 14710 | |
| 14711 | return result.promise; |
| 14712 | }, |
| 14713 | |
| 14714 | "catch": function(callback) { |
| 14715 | return this.then(null, callback); |
| 14716 | }, |
| 14717 | |
| 14718 | "finally": function(callback, progressBack) { |
| 14719 | return this.then(function(value) { |
| 14720 | return handleCallback(value, true, callback); |
| 14721 | }, function(error) { |
| 14722 | return handleCallback(error, false, callback); |
| 14723 | }, progressBack); |
| 14724 | } |
no test coverage detected