@@ -17,8 +17,33 @@ var DEFAULTS = {
17
17
maxAttempts : 5 , // try 5 times
18
18
retryDelay : 5000 , // wait for 5s before trying again
19
19
fullResponse : true , // resolve promise with the full response object
20
+ promiseFactory : defaultPromiseFactory // Function to use a different promise implementation library
20
21
} ;
21
22
23
+ // Default promise factory wich use bluebird
24
+ function defaultPromiseFactory ( resolver ) {
25
+ return new Promise ( resolver ) ;
26
+ }
27
+
28
+ /**
29
+ * It calls the promiseFactory function passing it the resolver for the promise
30
+ *
31
+ * @param {Object } requestInstance - The Request Retry instance
32
+ * @param {Function } promiseFactoryFn - The Request Retry instance
33
+ * @return {Object } - The promise instance
34
+ */
35
+ function makePromise ( requestInstance , promiseFactoryFn ) {
36
+
37
+ // Resolver function wich assigns the promise (resolve, reject) functions
38
+ // to the requestInstance
39
+ function resolver ( resolve , reject ) {
40
+ this . _resolve = resolve ;
41
+ this . _reject = reject ;
42
+ }
43
+
44
+ return promiseFactoryFn ( resolver . bind ( requestInstance ) ) ;
45
+ }
46
+
22
47
function Request ( options , f , retryConfig ) {
23
48
this . maxAttempts = retryConfig . maxAttempts ;
24
49
this . retryDelay = retryConfig . retryDelay ;
@@ -44,10 +69,7 @@ function Request(options, f, retryConfig) {
44
69
45
70
// create the promise only when no callback was provided
46
71
if ( ! this . _callback ) {
47
- this . _promise = new Promise ( function ( resolve , reject ) {
48
- this . _resolve = resolve ;
49
- this . _reject = reject ;
50
- } . bind ( this ) ) ;
72
+ this . _promise = makePromise ( this , retryConfig . promiseFactory ) ;
51
73
}
52
74
53
75
this . reply = function requestRetryReply ( err , response , body ) {
@@ -100,7 +122,7 @@ Request.prototype.abort = function () {
100
122
} ) ;
101
123
102
124
// expose promise methods
103
- [ 'then' , 'catch' , 'finally' ] . forEach ( function ( promiseMethod ) {
125
+ [ 'then' , 'catch' , 'finally' , 'fail' , 'done' ] . forEach ( function ( promiseMethod ) {
104
126
Request . prototype [ promiseMethod ] = function exposedPromiseMethod ( ) {
105
127
if ( this . _callback ) {
106
128
throw new Error ( 'A callback was provided but waiting a promise, use only one pattern' ) ;
0 commit comments