vercel / async-retry

Retrying made simple, easy and async
https://npmjs.com/async-retry
MIT License
1.85k stars 53 forks source link

feat: add support for aborting retrying using `AbortSignal` #105

Closed swain closed 1 year ago

swain commented 1 year ago

Motivation

This PR adds support for aborting retrying using the now-common AbortController / AbortSignal utilities via a new signal option.

This API allows for more flexibility in use cases that may need to imperatively stop the retrying from outside of the retried function.

Example

import retry from 'async-retry';

const controller = new AbortController();

// This will stop the retrying after 500ms.
setTimeout(
  () => controller.abort(new Error('end retrying')),
  500
)

await retry(
  async () => {
    await wait(100)
    throw new Error('some failure')
  },
  {
    forever: true,
    signal: controller.signal
  }
)
swain commented 1 year ago

@leo @leerob Is this package still maintained? If so, I'd like to propose this PR as a new feature.

Note: currently, CI is failing on the main branch due to a test that has become broken. I've put up a separate PR to fix that issue: #104.

swain commented 1 year ago

@rauchg Missed tagging you -- looks like you are one of the original authors as well.