diff --git a/src/promise-retry.js b/src/promise-retry.js index 94c24618a..0b174b4e3 100644 --- a/src/promise-retry.js +++ b/src/promise-retry.js @@ -17,6 +17,7 @@ async function promiseRetry(options, asyncFunction) { return await asyncFunction(i+1 /* attempt */); } catch (error) { if (i === times - 1) throw error; + if (options.retry && !options.retry(error)) throw error; // no more retry if (options.debug) options.debug(`Attempt ${i+1} failed. Will retry: ${error.message}`); await delay(interval); } diff --git a/src/test/promise-retry-test.js b/src/test/promise-retry-test.js index 8ebe1f54d..0a19147c8 100644 --- a/src/test/promise-retry-test.js +++ b/src/test/promise-retry-test.js @@ -35,4 +35,15 @@ describe('promiseRetry', function () { expect(result).to.be(42); }); + + it('can abort with 1 try only', async function () { + let tryCount = 0; + const [error] = await safe(promiseRetry({ times: 5, interval: 1000, retry: () => false }, async () => { + ++tryCount; + throw tryCount; + })); + + expect(tryCount).to.be(1); + expect(error).to.be(1); + }); });