* 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)
| 13060 | * @returns {object} Promise manager. |
| 13061 | */ |
| 13062 | function qFactory(nextTick, exceptionHandler) { |
| 13063 | var $qMinErr = minErr('$q', TypeError); |
| 13064 | function callOnce(self, resolveFn, rejectFn) { |
| 13065 | var called = false; |
| 13066 | function wrap(fn) { |
| 13067 | return function(value) { |
| 13068 | if (called) return; |
| 13069 | called = true; |
| 13070 | fn.call(self, value); |
| 13071 | }; |
| 13072 | } |
| 13073 | |
| 13074 | return [wrap(resolveFn), wrap(rejectFn)]; |
| 13075 | } |
| 13076 | |
| 13077 | /** |
| 13078 | * @ngdoc method |
| 13079 | * @name ng.$q#defer |
| 13080 | * @kind function |
| 13081 | * |
| 13082 | * @description |
| 13083 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 13084 | * |
| 13085 | * @returns {Deferred} Returns a new instance of deferred. |
| 13086 | */ |
| 13087 | var defer = function() { |
| 13088 | return new Deferred(); |
| 13089 | }; |
| 13090 | |
| 13091 | function Promise() { |
| 13092 | this.$$state = { status: 0 }; |
| 13093 | } |
| 13094 | |
| 13095 | Promise.prototype = { |
| 13096 | then: function(onFulfilled, onRejected, progressBack) { |
| 13097 | var result = new Deferred(); |
| 13098 | |
| 13099 | this.$$state.pending = this.$$state.pending || []; |
| 13100 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 13101 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 13102 | |
| 13103 | return result.promise; |
| 13104 | }, |
| 13105 | |
| 13106 | "catch": function(callback) { |
| 13107 | return this.then(null, callback); |
| 13108 | }, |
| 13109 | |
| 13110 | "finally": function(callback, progressBack) { |
| 13111 | return this.then(function(value) { |
| 13112 | return handleCallback(value, true, callback); |
| 13113 | }, function(error) { |
| 13114 | return handleCallback(error, false, callback); |
| 13115 | }, progressBack); |
| 13116 | } |
| 13117 | }; |
| 13118 | |
| 13119 | //Faster, more basic than angular.bind http://jsperf.com/angular-bind-vs-custom-vs-native |
no test coverage detected