Catch error thrown in fallback (#8)

This commit is contained in:
Ægir Örn Símonarson 2017-11-28 20:05:54 +00:00 committed by Sindre Sorhus
parent 63a486a0f2
commit 7fc1cb3dd2
2 changed files with 8 additions and 1 deletions

View File

@ -15,7 +15,11 @@ module.exports = (promise, ms, fallback) => new Promise((resolve, reject) => {
const timer = setTimeout(() => {
if (typeof fallback === 'function') {
resolve(fallback());
try {
resolve(fallback());
} catch (err) {
reject(err);
}
return;
}

View File

@ -30,6 +30,9 @@ test('fallback argument', async t => {
await t.throws(m(delay(200), 50, 'rainbow'), 'rainbow');
await t.throws(m(delay(200), 50, new RangeError('cake')), RangeError);
await t.throws(m(delay(200), 50, () => Promise.reject(fixtureErr)), fixtureErr.message);
await t.throws(m(delay(200), 50, () => {
throw new RangeError('cake');
}), RangeError);
});
test('calls `.cancel()` on promise when it exists', async t => {