* 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)
| 17939 | * @returns {object} Promise manager. |
| 17940 | */ |
| 17941 | function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) { |
| 17942 | var $qMinErr = minErr('$q', TypeError); |
| 17943 | var queueSize = 0; |
| 17944 | var checkQueue = []; |
| 17945 | |
| 17946 | /** |
| 17947 | * @ngdoc method |
| 17948 | * @name ng.$q#defer |
| 17949 | * @kind function |
| 17950 | * |
| 17951 | * @description |
| 17952 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 17953 | * |
| 17954 | * @returns {Deferred} Returns a new instance of deferred. |
| 17955 | */ |
| 17956 | function defer() { |
| 17957 | return new Deferred(); |
| 17958 | } |
| 17959 | |
| 17960 | function Deferred() { |
| 17961 | var promise = this.promise = new Promise(); |
| 17962 | //Non prototype methods necessary to support unbound execution :/ |
| 17963 | this.resolve = function(val) { resolvePromise(promise, val); }; |
| 17964 | this.reject = function(reason) { rejectPromise(promise, reason); }; |
| 17965 | this.notify = function(progress) { notifyPromise(promise, progress); }; |
| 17966 | } |
| 17967 | |
| 17968 | |
| 17969 | function Promise() { |
| 17970 | this.$$state = { status: 0 }; |
| 17971 | } |
| 17972 | |
| 17973 | extend(Promise.prototype, { |
| 17974 | then: function(onFulfilled, onRejected, progressBack) { |
| 17975 | if (isUndefined(onFulfilled) && isUndefined(onRejected) && isUndefined(progressBack)) { |
| 17976 | return this; |
| 17977 | } |
| 17978 | var result = new Promise(); |
| 17979 | |
| 17980 | this.$$state.pending = this.$$state.pending || []; |
| 17981 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 17982 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 17983 | |
| 17984 | return result; |
| 17985 | }, |
| 17986 | |
| 17987 | 'catch': function(callback) { |
| 17988 | return this.then(null, callback); |
| 17989 | }, |
| 17990 | |
| 17991 | 'finally': function(callback, progressBack) { |
| 17992 | return this.then(function(value) { |
| 17993 | return handleCallback(value, resolve, callback); |
| 17994 | }, function(error) { |
| 17995 | return handleCallback(error, reject, callback); |
| 17996 | }, progressBack); |
| 17997 | } |
| 17998 | }); |
no test coverage detected