UNPKG

1.15 kBJavaScriptView Raw
1'use strict';
2
3var Promise = global.Promise;
4
5/// encapsulate a method with a node-style callback in a Promise
6/// @param {object} 'this' of the encapsulated function
7/// @param {function} function to be encapsulated
8/// @param {Array-like} args to be passed to the called function
9/// @return {Promise} a Promise encapsulating the function
10module.exports.promise = function (fn, context, args) {
11
12 if (!Array.isArray(args)) {
13 args = Array.prototype.slice.call(args);
14 }
15
16 if (typeof fn !== 'function') {
17 return Promise.reject(new Error('fn must be a function'));
18 }
19
20 return new Promise(function(resolve, reject) {
21 args.push(function(err, data) {
22 if (err) {
23 reject(err);
24 } else {
25 resolve(data);
26 }
27 });
28
29 fn.apply(context, args);
30 });
31};
32
33/// @param {err} the error to be thrown
34module.exports.reject = function (err) {
35 return Promise.reject(err);
36};
37
38/// changes the promise implementation that bcrypt uses
39/// @param {Promise} the implementation to use
40module.exports.use = function(promise) {
41 Promise = promise;
42};