* 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)
| 13241 | * @returns {object} Promise manager. |
| 13242 | */ |
| 13243 | function qFactory(nextTick, exceptionHandler) { |
| 13244 | var $qMinErr = minErr('$q', TypeError); |
| 13245 | function callOnce(self, resolveFn, rejectFn) { |
| 13246 | var called = false; |
| 13247 | function wrap(fn) { |
| 13248 | return function(value) { |
| 13249 | if (called) return; |
| 13250 | called = true; |
| 13251 | fn.call(self, value); |
| 13252 | }; |
| 13253 | } |
| 13254 | |
| 13255 | return [wrap(resolveFn), wrap(rejectFn)]; |
| 13256 | } |
| 13257 | |
| 13258 | /** |
| 13259 | * @ngdoc method |
| 13260 | * @name ng.$q#defer |
| 13261 | * @kind function |
| 13262 | * |
| 13263 | * @description |
| 13264 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 13265 | * |
| 13266 | * @returns {Deferred} Returns a new instance of deferred. |
| 13267 | */ |
| 13268 | var defer = function() { |
| 13269 | return new Deferred(); |
| 13270 | }; |
| 13271 | |
| 13272 | function Promise() { |
| 13273 | this.$$state = { status: 0 }; |
| 13274 | } |
| 13275 | |
| 13276 | Promise.prototype = { |
| 13277 | then: function(onFulfilled, onRejected, progressBack) { |
| 13278 | var result = new Deferred(); |
| 13279 | |
| 13280 | this.$$state.pending = this.$$state.pending || []; |
| 13281 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 13282 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 13283 | |
| 13284 | return result.promise; |
| 13285 | }, |
| 13286 | |
| 13287 | "catch": function(callback) { |
| 13288 | return this.then(null, callback); |
| 13289 | }, |
| 13290 | |
| 13291 | "finally": function(callback, progressBack) { |
| 13292 | return this.then(function(value) { |
| 13293 | return handleCallback(value, true, callback); |
| 13294 | }, function(error) { |
| 13295 | return handleCallback(error, false, callback); |
| 13296 | }, progressBack); |
| 13297 | } |
| 13298 | }; |
| 13299 | |
| 13300 | //Faster, more basic than angular.bind http://jsperf.com/angular-bind-vs-custom-vs-native |
no test coverage detected