diff --git a/common/changes/@rushstack/node-core-library/user-danade-AddRetryCount_2025-01-08-22-19.json b/common/changes/@rushstack/node-core-library/user-danade-AddRetryCount_2025-01-08-22-19.json new file mode 100644 index 00000000000..af1a1aea877 --- /dev/null +++ b/common/changes/@rushstack/node-core-library/user-danade-AddRetryCount_2025-01-08-22-19.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@rushstack/node-core-library", + "comment": "Provide the `retryCount` parameter to actions executed using `Async.runWithRetriesAsync`", + "type": "patch" + } + ], + "packageName": "@rushstack/node-core-library" +} \ No newline at end of file diff --git a/common/reviews/api/node-core-library.api.md b/common/reviews/api/node-core-library.api.md index 3dc3e2214ee..f1e2a9d0e93 100644 --- a/common/reviews/api/node-core-library.api.md +++ b/common/reviews/api/node-core-library.api.md @@ -616,11 +616,8 @@ export interface IRealNodeModulePathResolverOptions { // @public (undocumented) export interface IRunWithRetriesOptions { - // (undocumented) - action: () => Promise | TResult; - // (undocumented) + action: (retryCount: number) => Promise | TResult; maxRetries: number; - // (undocumented) retryDelayMs?: number; } diff --git a/libraries/node-core-library/src/Async.ts b/libraries/node-core-library/src/Async.ts index 227dd2480e9..3451b48f101 100644 --- a/libraries/node-core-library/src/Async.ts +++ b/libraries/node-core-library/src/Async.ts @@ -32,8 +32,20 @@ export interface IAsyncParallelismOptions { * @public */ export interface IRunWithRetriesOptions { - action: () => Promise | TResult; + /** + * The action to be performed. The action is repeatedly executed until it completes without throwing or the + * maximum number of retries is reached. + * + * @param retryCount - The number of times the action has been retried. + */ + action: (retryCount: number) => Promise | TResult; + /** + * The maximum number of times the action should be retried. + */ maxRetries: number; + /** + * The delay in milliseconds between retries. + */ retryDelayMs?: number; } @@ -313,13 +325,13 @@ export class Async { maxRetries, retryDelayMs = 0 }: IRunWithRetriesOptions): Promise { - let retryCounter: number = 0; + let retryCount: number = 0; // eslint-disable-next-line no-constant-condition while (true) { try { - return await action(); + return await action(retryCount); } catch (e) { - if (++retryCounter > maxRetries) { + if (++retryCount > maxRetries) { throw e; } else if (retryDelayMs > 0) { await Async.sleepAsync(retryDelayMs);