Skip to content

Commit

Permalink
Improve readability, avoid repetition (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored and sindresorhus committed May 2, 2018
1 parent e64328e commit 6ffaeed
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,35 @@ const processFn = (fn, opts) => function () {
}

return new P((resolve, reject) => {
if (opts.errorFirst) {
args.push(function (err, result) {
if (opts.multiArgs) {
const results = new Array(arguments.length - 1);
if (opts.multiArgs) {
args.push(function (err) {
const results = new Array(arguments.length - 1);

for (let i = 1; i < arguments.length; i++) {
results[i - 1] = arguments[i];
}
for (let i = 0; i < arguments.length; i++) {
results[i] = arguments[i];
}

if (opts.errorFirst) {
if (err) {
results.unshift(err);
reject(results);
} else {
results.shift();
resolve(results);
}
} else if (err) {
reject(err);
} else {
resolve(result);
resolve(results);
}
});
} else {
args.push(function (result) {
if (opts.multiArgs) {
const results = new Array(arguments.length - 1);

for (let i = 0; i < arguments.length; i++) {
results[i] = arguments[i];
}

resolve(results);
} else if (opts.errorFirst) {
args.push((err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
} else {
args.push(resolve);
}

fn.apply(this, args);
Expand Down

0 comments on commit 6ffaeed

Please sign in to comment.