From 2ffb9f42dd6a5d251e7076041bca9ff45521f0cd Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Tue, 25 Feb 2025 01:36:27 -0800 Subject: [PATCH] Rename `options.value` to `options.placeholder` --- src/errorStrategies.js | 22 ++++++++++------------ test/errorStrategies.test.js | 4 ++-- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/errorStrategies.js b/src/errorStrategies.js index 4c9d08a..508fa4d 100644 --- a/src/errorStrategies.js +++ b/src/errorStrategies.js @@ -33,8 +33,8 @@ function placeholder(value) { /** * @typedef {Object} RetryOptions - * @property {string} [value] If provided, a placeholder value to set if the function still errors after the final iteration. - * If `value` is unset, the final error thrown by the function is re-thrown. + * @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. */ /** @@ -47,14 +47,13 @@ function placeholder(value) { * @throws If `value` is unset and the function throws an error after the final iteration, the error is re-thrown. */ function retry(iterations, options = {}) { - let { value } = options; return async (row, e, fn) => { while (iterations--) { try { return await fn(row); } catch { } } - if (value !== undefined) { - return placeholder(value)(row, e, fn); + if (options.placeholder !== undefined) { + return placeholder(options.placeholder)(row, e, fn); } throw e; } @@ -65,8 +64,8 @@ function retry(iterations, options = {}) { * @property {number} [maxExponent] If provided, the maximum exponent used to calculate the retry duration. In other words, * the `iterations` number at which the retry duration stops increasing. If `maxExponent` is unset, the retry duration * is unbounded and will increase after each retry. - * @property {string} [value] If provided, a placeholder value to set if the function still errors after the final iteration. - * If `value` is unset, the final error thrown by the function is re-thrown. + * @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. */ /** @@ -80,19 +79,18 @@ function retry(iterations, options = {}) { * @throws If `value` is unset and the function throws an error after the final iteration, the error is re-thrown. */ function backoff(iterations, options = {}) { - let { maxExponent, value } = options; - maxExponent ??= Infinity; + options.maxExponent ??= Infinity; return async (row, e, fn) => { for (let n = 0; n < iterations; n++) { - const exponent = n > maxExponent ? maxExponent : n; + const exponent = n > options.maxExponent ? options.maxExponent : n; const duration = (2 ** exponent) * 1000; // 1s, 2s, 4s, 8s, 16s, ... const jitter = Math.random() * 1000; await utils.sleep(duration + jitter); try { return await fn(row); } catch { } } - if (value !== undefined) { - return placeholder(value)(row, e, fn); + if (options.placeholder !== undefined) { + return placeholder(options.placeholder)(row, e, fn); } throw e; } diff --git a/test/errorStrategies.test.js b/test/errorStrategies.test.js index 2f275a1..54eda6e 100644 --- a/test/errorStrategies.test.js +++ b/test/errorStrategies.test.js @@ -139,7 +139,7 @@ suite('ErrorStrategies.retry', { concurrency: true }, () => { ['header'], ['0'] ]) - .pipeThrough(new CSVTransformer(fn, { onError: retry(1, { value: 'n/a' }) })) + .pipeThrough(new CSVTransformer(fn, { onError: retry(1, { placeholder: 'n/a' }) })) .pipeTo(csvStreamEqualWritable([ ['header'], ['n/a'] @@ -228,7 +228,7 @@ suite('ErrorStrategies.backoff', { concurrency: 1 }, () => { // TODO: These test ['header'], ['0'] ]) - .pipeThrough(new CSVTransformer(fn, { onError: backoff(3, { value: 'n/a' }) })) + .pipeThrough(new CSVTransformer(fn, { onError: backoff(3, { placeholder: 'n/a' }) })) .pipeTo(csvStreamEqualWritable([ ['header'], ['n/a'] -- 2.47.3