* 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)
| 17071 | * @returns {object} Promise manager. |
| 17072 | */ |
| 17073 | function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) { |
| 17074 | var $qMinErr = minErr('$q', TypeError); |
| 17075 | var queueSize = 0; |
| 17076 | var checkQueue = []; |
| 17077 | |
| 17078 | /** |
| 17079 | * @ngdoc method |
| 17080 | * @name ng.$q#defer |
| 17081 | * @kind function |
| 17082 | * |
| 17083 | * @description |
| 17084 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 17085 | * |
| 17086 | * @returns {Deferred} Returns a new instance of deferred. |
| 17087 | */ |
| 17088 | function defer() { |
| 17089 | return new Deferred(); |
| 17090 | } |
| 17091 | |
| 17092 | function Deferred() { |
| 17093 | var promise = this.promise = new Promise(); |
| 17094 | //Non prototype methods necessary to support unbound execution :/ |
| 17095 | this.resolve = function(val) { resolvePromise(promise, val); }; |
| 17096 | this.reject = function(reason) { rejectPromise(promise, reason); }; |
| 17097 | this.notify = function(progress) { notifyPromise(promise, progress); }; |
| 17098 | } |
| 17099 | |
| 17100 | |
| 17101 | function Promise() { |
| 17102 | this.$$state = { status: 0 }; |
| 17103 | } |
| 17104 | |
| 17105 | extend(Promise.prototype, { |
| 17106 | then: function(onFulfilled, onRejected, progressBack) { |
| 17107 | if (isUndefined(onFulfilled) && isUndefined(onRejected) && isUndefined(progressBack)) { |
| 17108 | return this; |
| 17109 | } |
| 17110 | var result = new Promise(); |
| 17111 | |
| 17112 | this.$$state.pending = this.$$state.pending || []; |
| 17113 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 17114 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 17115 | |
| 17116 | return result; |
| 17117 | }, |
| 17118 | |
| 17119 | 'catch': function(callback) { |
| 17120 | return this.then(null, callback); |
| 17121 | }, |
| 17122 | |
| 17123 | 'finally': function(callback, progressBack) { |
| 17124 | return this.then(function(value) { |
| 17125 | return handleCallback(value, resolve, callback); |
| 17126 | }, function(error) { |
| 17127 | return handleCallback(error, reject, callback); |
| 17128 | }, progressBack); |
| 17129 | } |
| 17130 | }); |
no test coverage detected