/**
* @typedef {Object} RetryOptions
+ * @property {number} [interval] The duration (in milliseconds) to wait in between each retry. The default value is `0`
+ * (retry immediately).
* @property {string} [placeholder] A placeholder value to set if the function still errors after the final iteration.
* If unset, the final error thrown by the function is re-thrown.
*/
* @throws If `value` is unset and the function throws an error after the final iteration, the error is re-thrown.
*/
function retry(iterations, options = {}) {
+ options.interval ??= 0;
return async (row, e, fn) => {
while (iterations--) {
+ await utils.sleep(options.interval);
try { return await fn(row); }
catch { }
}
[10]
]));
});
+ test('retries with interval', async (t) => {
+ assertSleep(t, { min: 10 * 1000, max: 10 * 1000 + 1000 });
+ const fn = retryFactory(11); // 1st try + 10 retries = pass on 11th with extra retries available
+ await createCSVMockStream([
+ ['header'],
+ ['0']
+ ])
+ .pipeThrough(new CSVTransformer(fn, { onError: retry(10, { interval: 1000 }) }))
+ .pipeTo(csvStreamEqualWritable([
+ ['header'],
+ [11]
+ ]));
+ });
test('sets placeholder if failed after last iteration and placeholder is given', async () => {
const fn = retryFactory(100);
await createCSVMockStream([