Skip to content

Commit

Permalink
Make the Promise interface complete by adding a .finally() method (#…
Browse files Browse the repository at this point in the history
…174)

Fixes #147
  • Loading branch information
bradfordlemley authored and sindresorhus committed Mar 4, 2019
1 parent 0f48671 commit f309669
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -316,6 +316,10 @@ module.exports = (command, args, options) => {

spawned.then = (onfulfilled, onrejected) => handlePromise().then(onfulfilled, onrejected);
spawned.catch = onrejected => handlePromise().catch(onrejected);
// eslint-disable-next-line no-use-extend-native/no-use-extend-native
if (Promise.prototype.finally) {
spawned.finally = onfinally => handlePromise().finally(onfinally);
}

return spawned;
};
Expand Down
36 changes: 36 additions & 0 deletions test.js
Expand Up @@ -519,3 +519,39 @@ test('removes exit handler on exit', async t => {
const included = ee.listeners('exit').includes(listener);
t.false(included);
});

// eslint-disable-next-line no-use-extend-native/no-use-extend-native
if (Promise.prototype.finally) {
test('finally function is executed on success', async t => {
let called = false;
const {stdout} = await m('noop', ['foo']).finally(() => {
called = true;
});
t.is(called, true);
t.is(stdout, 'foo');
});

test('finally function is executed on failure', async t => {
let called = false;
const err = await t.throws(m('exit', ['2']).finally(() => {
called = true;
}));
t.is(called, true);
t.is(typeof err.stdout, 'string');
t.is(typeof err.stderr, 'string');
});

test('throw in finally function bubbles up on success', async t => {
const result = await t.throws(m('noop', ['foo']).finally(() => {
throw new Error('called');
}));
t.is(result.message, 'called');
});

test('throw in finally bubbles up on error', async t => {
const result = await t.throws(m('exit', ['2']).finally(() => {
throw new Error('called');
}));
t.is(result.message, 'called');
});
}

0 comments on commit f309669

Please sign in to comment.