From 7fc1cb3dd20ee3368901725f76e20295350873e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86gir=20=C3=96rn=20S=C3=ADmonarson?= Date: Tue, 28 Nov 2017 20:05:54 +0000 Subject: [PATCH] Catch error thrown in fallback (#8) --- index.js | 6 +++++- test.js | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ab2d294..8393646 100644 --- a/index.js +++ b/index.js @@ -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; } diff --git a/test.js b/test.js index 8aa3879..b54cac1 100644 --- a/test.js +++ b/test.js @@ -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 => {