MCPcopy Index your code
hub / github.com/callstack/agent-device / retryWithPolicy

Function retryWithPolicy

src/utils/retry.ts:76–172  ·  view source on GitHub ↗
(
  fn: (context: RetryAttemptContext) => Promise<T>,
  policy: Partial<RetryPolicy> = {},
  options: {
    deadline?: Deadline;
    phase?: string;
    signal?: AbortSignal;
    classifyReason?: (error: unknown) => string | undefined;
    onEvent?: (event: RetryTelemetryEvent) => void;
  } = {},
)

Source from the content-addressed store, hash-verified

74}
75
76export async function retryWithPolicy<T>(
77 fn: (context: RetryAttemptContext) => Promise<T>,
78 policy: Partial<RetryPolicy> = {},
79 options: {
80 deadline?: Deadline;
81 phase?: string;
82 signal?: AbortSignal;
83 classifyReason?: (error: unknown) => string | undefined;
84 onEvent?: (event: RetryTelemetryEvent) => void;
85 } = {},
86): Promise<T> {
87 const merged: RetryPolicy = {
88 maxAttempts: policy.maxAttempts ?? defaultOptions.attempts,
89 baseDelayMs: policy.baseDelayMs ?? defaultOptions.baseDelayMs,
90 maxDelayMs: policy.maxDelayMs ?? defaultOptions.maxDelayMs,
91 jitter: policy.jitter ?? defaultOptions.jitter,
92 shouldRetry: policy.shouldRetry,
93 };
94 let lastError: unknown;
95 for (let attempt = 1; attempt <= merged.maxAttempts; attempt += 1) {
96 if (options.signal?.aborted) {
97 throw new AppError('COMMAND_FAILED', 'request canceled', { reason: 'request_canceled' });
98 }
99 if (options.deadline?.isExpired() && attempt > 1) break;
100 try {
101 const result = await fn({
102 attempt,
103 maxAttempts: merged.maxAttempts,
104 deadline: options.deadline,
105 });
106 options.onEvent?.({
107 phase: options.phase,
108 event: 'succeeded',
109 attempt,
110 maxAttempts: merged.maxAttempts,
111 elapsedMs: options.deadline?.elapsedMs(),
112 remainingMs: options.deadline?.remainingMs(),
113 });
114 publishRetryEvent({
115 phase: options.phase,
116 event: 'succeeded',
117 attempt,
118 maxAttempts: merged.maxAttempts,
119 elapsedMs: options.deadline?.elapsedMs(),
120 remainingMs: options.deadline?.remainingMs(),
121 });
122 return result;
123 } catch (err) {
124 lastError = err;
125 const reason = options.classifyReason?.(err);
126 const failedEvent: RetryTelemetryEvent = {
127 phase: options.phase,
128 event: 'attempt_failed',
129 attempt,
130 maxAttempts: merged.maxAttempts,
131 elapsedMs: options.deadline?.elapsedMs(),
132 remainingMs: options.deadline?.remainingMs(),
133 reason,

Callers 7

retry.test.tsFile · 0.90
waitForAndroidBootFunction · 0.90
launchIosSimulatorAppFunction · 0.90
ensureBootedSimulatorFunction · 0.90
waitForRunnerFunction · 0.90
withRetryFunction · 0.85

Calls 6

publishRetryEventFunction · 0.85
computeDelayFunction · 0.85
isExpiredMethod · 0.80
elapsedMsMethod · 0.80
remainingMsMethod · 0.80
sleepFunction · 0.70

Tested by

no test coverage detected