Skip to content
Permalink
Browse files
Make multiArgs option apply on rejections too (#35)
  • Loading branch information
mightyiam authored and sindresorhus committed Apr 18, 2017
1 parent f66eebc commit 30116d9ca0e683f3e2c184bfc53a7061d0efbba3
Showing 2 changed files with 18 additions and 4 deletions.
@@ -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);
}
@@ -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(() => {
@@ -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');
});
@@ -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');
});

0 comments on commit 30116d9

Please sign in to comment.