Skip to content

Commit

Permalink
Make multiArgs option apply on rejections too (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
mightyiam authored and sindresorhus committed Apr 18, 2017
1 parent f66eebc commit 30116d9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ const processFn = (fn, opts) => function () {

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

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

resolve(results);
if (err) {
results.unshift(err);
reject(results);
} else {
resolve(results);
}
} else if (err) {
reject(err);
} else {
resolve(result);
}
Expand Down
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import pinkiePromise from 'pinkie-promise';
import fn from './';

const fixture = cb => setImmediate(() => cb(null, 'unicorn'));
const fixture1 = cb => setImmediate(() => cb('error', 'unicorn', 'rainbow'));
const fixture2 = (x, cb) => setImmediate(() => cb(null, x));
const fixture3 = cb => setImmediate(() => cb(null, 'unicorn', 'rainbow'));
const fixture4 = cb => setImmediate(() => {
Expand All @@ -30,6 +31,10 @@ test('main', async t => {
t.is(await fn(fixture)(), 'unicorn');
});

test('error', async t => {
t.is(await fn(fixture1)().catch(err => err), 'error');
});

test('pass argument', async t => {
t.is(await fn(fixture2)('rainbow'), 'rainbow');
});
Expand All @@ -42,6 +47,10 @@ test('multiArgs option', async t => {
t.deepEqual(await fn(fixture3, {multiArgs: true})(), ['unicorn', 'rainbow']);
});

test('multiArgs option — rejection', async t => {
t.deepEqual(await fn(fixture1, {multiArgs: true})().catch(err => err), ['error', 'unicorn', 'rainbow']);
});

test('wrap core method', async t => {
t.is(JSON.parse(await fn(fs.readFile)('package.json')).name, 'pify');
});
Expand Down

0 comments on commit 30116d9

Please sign in to comment.