Skip to content

Commit

Permalink
feat: use util.promisify when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 15, 2018
1 parent 696abf8 commit a27dcc2
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
'use strict';

const processFn = (fn, options) => function (...args) {
const P = options.promiseModule;
const nativePromisify = require('util').promisify;

return new P((resolve, reject) => {
if (options.multiArgs) {
args.push((...result) => {
if (options.errorFirst) {
if (result[0]) {
reject(result);
function processFn (fn, options) {
if (
nativePromisify // Util.promisify supported
&& options.native !== false // Native not disabled with options
&& !options.multiArgs // No multiArgs mode
&& options.errorFirst !== false // Only errorFirst mode is supported
) {
return nativePromisify(fn);
}

return function (...args) {
const P = options.promiseModule;

return new P((resolve, reject) => {
if (options.multiArgs) {
args.push((...result) => {
if (options.errorFirst) {
if (result[0]) {
reject(result);
} else {
result.shift();
resolve(result);
}
} else {
resolve(result);
}
});
} else if (options.errorFirst) {
args.push((error, result) => {
if (error) {
reject(error);
} else {
result.shift();
resolve(result);
}
} else {
resolve(result);
}
});
} else if (options.errorFirst) {
args.push((error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
} else {
args.push(resolve);
}
});
} else {
args.push(resolve);
}

fn.apply(this, args);
});
};
fn.apply(this, args);
});
};
}

module.exports = (input, options) => {
options = Object.assign({
Expand Down

0 comments on commit a27dcc2

Please sign in to comment.