Skip to content

Commit dab435e

Browse files
gurpreetatwalrecrsn
authored andcommittedApr 6, 2018
install and use any-promise (#504)
* export a use function from lib/promises closes #478 the `use` function can be used to change the promise implementation that is used by the library
1 parent aac593c commit dab435e

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed
 

‎lib/promises.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
'use strict';
22

3+
var Promise = global.Promise;
4+
35
/// encapsulate a method with a node-style callback in a Promise
46
/// @param {object} 'this' of the encapsulated function
57
/// @param {function} function to be encapsulated
68
/// @param {Array-like} args to be passed to the called function
7-
/// @return {Promise} a Promise encapuslaing the function
9+
/// @return {Promise} a Promise encapsulating the function
810
module.exports.promise = function (fn, context, args) {
911

1012
if (!Array.isArray(args)) {
@@ -32,3 +34,9 @@ module.exports.promise = function (fn, context, args) {
3234
module.exports.reject = function (err) {
3335
return Promise.reject(err);
3436
};
37+
38+
/// changes the promise implementation that bcrypt uses
39+
/// @param {Promise} the implementation to use
40+
module.exports.use = function(promise) {
41+
Promise = promise;
42+
};

‎test/promise.js

+32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var bcrypt = require('../bcrypt');
2+
var promises = require('../lib/promises');
23

34
var fail = function(assert, error) {
45
assert.ok(false, error);
@@ -215,6 +216,37 @@ if (typeof Promise !== 'undefined') {
215216
}).then(function() {
216217
assert.done();
217218
});
219+
},
220+
test_change_promise_impl_reject: function(assert) {
221+
222+
promises.use({
223+
reject: function() {
224+
return 'mock';
225+
}
226+
});
227+
228+
assert.equal(promises.reject(), 'mock');
229+
230+
// need to reset the promise implementation because of require cache
231+
promises.use(global.Promise);
232+
assert.done();
233+
234+
},
235+
test_change_promise_impl_promise: function(assert) {
236+
237+
promises.use({
238+
reject: function(err) {
239+
assert.equal(err.message, 'fn must be a function');
240+
return 'mock';
241+
}
242+
});
243+
244+
assert.equal(promises.promise('', '', ''), 'mock');
245+
246+
// need to reset the promise implementation because of require cache
247+
promises.use(global.Promise);
248+
assert.done();
249+
218250
}
219251
};
220252
}

0 commit comments

Comments
 (0)
Please sign in to comment.