Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f3a763f

Browse files
committed
feat($q): add streamlined ES6-style interface for using $q
This potentially helps lead the way towards a more performant fly-weight implementation, as discussed earlier in the year. Using a constructor means we can put things in the prototype chain, and essentially treat $q as a Promise class, and reuse methods as appropriate. Short of that, I feel this style is slightly more convenient and streamlined, compared with the older API. Closes #8311 Closes #6427 (I know it's not really the solution asked for in #6427, sorry!)
1 parent c54228f commit f3a763f

File tree

2 files changed

+676
-6
lines changed

2 files changed

+676
-6
lines changed

src/ng/q.js

+77-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,46 @@
88
* @description
99
* A promise/deferred implementation inspired by [Kris Kowal's Q](https://github.com/kriskowal/q).
1010
*
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+
*
1151
* [The CommonJS Promise proposal](http://wiki.commonjs.org/wiki/Promises) describes a promise as an
1252
* interface for interacting with an object that represents the result of an action that is
1353
* performed asynchronously, and may or may not be finished at any given point in time.
@@ -54,7 +94,6 @@
5494
* For more on this please see the [Q documentation](https://github.com/kriskowal/q) especially the
5595
* section on serial or parallel joining of promises.
5696
*
57-
*
5897
* # The Deferred API
5998
*
6099
* A new instance of deferred is constructed by calling `$q.defer()`.
@@ -163,6 +202,12 @@
163202
* expect(resolvedValue).toEqual(123);
164203
* }));
165204
* ```
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.
166211
*/
167212
function $QProvider() {
168213

@@ -519,10 +564,36 @@ function qFactory(nextTick, exceptionHandler) {
519564
return deferred.promise;
520565
}
521566

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;
527591
};
592+
593+
$Q.defer = defer;
594+
$Q.reject = reject;
595+
$Q.when = when;
596+
$Q.all = all;
597+
598+
return $Q;
528599
}

0 commit comments

Comments
 (0)