* 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)
| 14490 | * @returns {object} Promise manager. |
| 14491 | */ |
| 14492 | function qFactory(nextTick, exceptionHandler) { |
| 14493 | var $qMinErr = minErr('$q', TypeError); |
| 14494 | function callOnce(self, resolveFn, rejectFn) { |
| 14495 | var called = false; |
| 14496 | function wrap(fn) { |
| 14497 | return function(value) { |
| 14498 | if (called) return; |
| 14499 | called = true; |
| 14500 | fn.call(self, value); |
| 14501 | }; |
| 14502 | } |
| 14503 | |
| 14504 | return [wrap(resolveFn), wrap(rejectFn)]; |
| 14505 | } |
| 14506 | |
| 14507 | /** |
| 14508 | * @ngdoc method |
| 14509 | * @name ng.$q#defer |
| 14510 | * @kind function |
| 14511 | * |
| 14512 | * @description |
| 14513 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 14514 | * |
| 14515 | * @returns {Deferred} Returns a new instance of deferred. |
| 14516 | */ |
| 14517 | var defer = function() { |
| 14518 | return new Deferred(); |
| 14519 | }; |
| 14520 | |
| 14521 | function Promise() { |
| 14522 | this.$$state = { status: 0 }; |
| 14523 | } |
| 14524 | |
| 14525 | Promise.prototype = { |
| 14526 | then: function(onFulfilled, onRejected, progressBack) { |
| 14527 | var result = new Deferred(); |
| 14528 | |
| 14529 | this.$$state.pending = this.$$state.pending || []; |
| 14530 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 14531 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 14532 | |
| 14533 | return result.promise; |
| 14534 | }, |
| 14535 | |
| 14536 | "catch": function(callback) { |
| 14537 | return this.then(null, callback); |
| 14538 | }, |
| 14539 | |
| 14540 | "finally": function(callback, progressBack) { |
| 14541 | return this.then(function(value) { |
| 14542 | return handleCallback(value, true, callback); |
| 14543 | }, function(error) { |
| 14544 | return handleCallback(error, false, callback); |
| 14545 | }, progressBack); |
| 14546 | } |
| 14547 | }; |
| 14548 | |
| 14549 | //Faster, more basic than angular.bind http://jsperf.com/angular-bind-vs-custom-vs-native |
no test coverage detected