|
8 | 8 | * @description
|
9 | 9 | * A promise/deferred implementation inspired by [Kris Kowal's Q](https://github.com/kriskowal/q).
|
10 | 10 | *
|
| 11 | + * $q can be used in two fashions --- One, which is more similar to Kris Kowal's Q or jQuery's Deferred |
| 12 | + * implementations, the other resembles ES6 promises to some degree. |
| 13 | + * |
| 14 | + * # $q constructor |
| 15 | + * |
| 16 | + * The streamlined ES6 style promise is essentially just using $q as a constructor which takes a `resolver` |
| 17 | + * function as the first argument). This is similar to the native Promise implementation from ES6 Harmony, |
| 18 | + * see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). |
| 19 | + * |
| 20 | + * While the constructor-style use is supported, not all of the supporting methods from Harmony promises are |
| 21 | + * available yet. |
| 22 | + * |
| 23 | + * It can be used like so: |
| 24 | + * |
| 25 | + * ```js |
| 26 | + * return $q(function(resolve, reject) { |
| 27 | + * // perform some asynchronous operation, resolve or reject the promise when appropriate. |
| 28 | + * setInterval(function() { |
| 29 | + * if (pollStatus > 0) { |
| 30 | + * resolve(polledValue); |
| 31 | + * } else if (pollStatus < 0) { |
| 32 | + * reject(polledValue); |
| 33 | + * } else { |
| 34 | + * pollStatus = pollAgain(function(value) { |
| 35 | + * polledValue = value; |
| 36 | + * }); |
| 37 | + * } |
| 38 | + * }, 10000); |
| 39 | + * }). |
| 40 | + * then(function(value) { |
| 41 | + * // handle success |
| 42 | + * }, function(reason) { |
| 43 | + * // handle failure |
| 44 | + * }); |
| 45 | + * ``` |
| 46 | + * |
| 47 | + * Note, progress/notify callbacks are not currently supported via the ES6-style interface. |
| 48 | + * |
| 49 | + * However, the more traditional CommonJS style usage is still available, and documented below. |
| 50 | + * |
11 | 51 | * [The CommonJS Promise proposal](http://wiki.commonjs.org/wiki/Promises) describes a promise as an
|
12 | 52 | * interface for interacting with an object that represents the result of an action that is
|
13 | 53 | * performed asynchronously, and may or may not be finished at any given point in time.
|
|
54 | 94 | * For more on this please see the [Q documentation](https://github.com/kriskowal/q) especially the
|
55 | 95 | * section on serial or parallel joining of promises.
|
56 | 96 | *
|
57 |
| - * |
58 | 97 | * # The Deferred API
|
59 | 98 | *
|
60 | 99 | * A new instance of deferred is constructed by calling `$q.defer()`.
|
|
163 | 202 | * expect(resolvedValue).toEqual(123);
|
164 | 203 | * }));
|
165 | 204 | * ```
|
| 205 | + * |
| 206 | + * @param {function(function, function)} resolver Function which is responsible for resolving or |
| 207 | + * rejecting the newly created promise. The first parameteter is a function which resolves the |
| 208 | + * promise, the second parameter is a function which rejects the promise. |
| 209 | + * |
| 210 | + * @returns {Promise} The newly created promise. |
166 | 211 | */
|
167 | 212 | function $QProvider() {
|
168 | 213 |
|
@@ -519,10 +564,36 @@ function qFactory(nextTick, exceptionHandler) {
|
519 | 564 | return deferred.promise;
|
520 | 565 | }
|
521 | 566 |
|
522 |
| - return { |
523 |
| - defer: defer, |
524 |
| - reject: reject, |
525 |
| - when: when, |
526 |
| - all: all |
| 567 | + var $Q = function Q(resolver) { |
| 568 | + if (!isFunction(resolver)) { |
| 569 | + // TODO(@caitp): minErr this |
| 570 | + throw new TypeError('Expected resolverFn'); |
| 571 | + } |
| 572 | + |
| 573 | + if (!(this instanceof Q)) { |
| 574 | + // More useful when $Q is the Promise itself. |
| 575 | + return new Q(resolver); |
| 576 | + } |
| 577 | + |
| 578 | + var deferred = defer(); |
| 579 | + |
| 580 | + function resolveFn(value) { |
| 581 | + deferred.resolve(value); |
| 582 | + } |
| 583 | + |
| 584 | + function rejectFn(reason) { |
| 585 | + deferred.reject(reason); |
| 586 | + } |
| 587 | + |
| 588 | + resolver(resolveFn, rejectFn); |
| 589 | + |
| 590 | + return deferred.promise; |
527 | 591 | };
|
| 592 | + |
| 593 | + $Q.defer = defer; |
| 594 | + $Q.reject = reject; |
| 595 | + $Q.when = when; |
| 596 | + $Q.all = all; |
| 597 | + |
| 598 | + return $Q; |
528 | 599 | }
|
0 commit comments