Skip to content
Myr

Make the Promise interface complete by adding a `.finally()` method (#174)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixes #147

authored by Bradford Lemley · committed by Sindre Sorhus · parent 0f48671 · Browse files · 2 files+40 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+40 added−0 removed

  • other:+40 added−0 removed

2 files+40 added−0 removed

modified:index.js+4 added
314 unmodified lines
315 handleInput(spawned, parsed.options.input);316317 spawned.then = (onfulfilled, onrejected) => handlePromise().then(onfulfilled, onrejected);318 spawned.catch = onrejected => handlePromise().catch(onrejected);319 // eslint-disable-next-line no-use-extend-native/no-use-extend-native320 if (Promise.prototype.finally) {321 spawned.finally = onfinally => handlePromise().finally(onfinally);322 }323324 return spawned;325};326
44 unmodified lines
modified:test.js+36 added
517 unmodified lines
518519 const included = ee.listeners('exit').includes(listener);520 t.false(included);521});522523// eslint-disable-next-line no-use-extend-native/no-use-extend-native524if (Promise.prototype.finally) {525 test('finally function is executed on success', async t => {526 let called = false;527 const {stdout} = await m('noop', ['foo']).finally(() => {528 called = true;529 });530 t.is(called, true);531 t.is(stdout, 'foo');532 });533534 test('finally function is executed on failure', async t => {535 let called = false;536 const err = await t.throws(m('exit', ['2']).finally(() => {537 called = true;538 }));539 t.is(called, true);540 t.is(typeof err.stdout, 'string');541 t.is(typeof err.stderr, 'string');542 });543544 test('throw in finally function bubbles up on success', async t => {545 const result = await t.throws(m('noop', ['foo']).finally(() => {546 throw new Error('called');547 }));548 t.is(result.message, 'called');549 });550551 test('throw in finally bubbles up on error', async t => {552 const result = await t.throws(m('exit', ['2']).finally(() => {553 throw new Error('called');554 }));555 t.is(result.message, 'called');556 });557}