recoverable-iterator
If an iterator errors, restart and continue.
Install
npm install recoverable-iterator
Usage
const recoverable = // I fail after the third iterationconst failingIterable = { let i = 0 return { if i > 3 throw 'BOOM' return done: false value: i++ } }const iterable = 1 2 3 // Pass a function to create or re-create the iterable on errorconst it = console // { done: false, value: 0 }console // { done: false, value: 1 }console // { done: false, value: 2 }console // { done: false, value: 3 }console // { done: false, value: 1 }console // { done: false, value: 2 }console // { done: false, value: 3 }console // { done: true, value: undefined }
API
const recoverable =
recoverable(create)
Returns an async iterator (also iterable) that can recover when the iterator returned by the create
function throws an error. When this happens, create
will be called again (and passed the error object) to re-create a new iterator or re-throw the error if it's fatal.
create
can return a promise so that delay and backoff can occur or if creation of the iterator just so happens to be async.
Parameters
Name | Type | Description |
---|---|---|
create | 'Function' | Factory for creating and returning the iterator that should be recoverable. After the first call subsequent calls receive an Error parameter which can be used to determine if recovery should happen or not |
Returns
Type | Description |
---|---|
Iterator |
A "recoverable" async iterator. |
Contribute
Feel free to dive in! Open an issue or submit PRs.
License
MIT © Alan Shaw