...
/Solution: Decorate a Retryable Operation with Metrics
Solution: Decorate a Retryable Operation with Metrics
Implement a retry mechanism for unreliable async operations, then wrap it with logging and timing decorators to track execution and stability.
We'll cover the following...
We'll cover the following...
Solution explanation
Lines 2–5: We define
flakyOperation, an async function that fails about 75% of the time. It simulates a flaky network or external API call.Lines 8–19: We implement
retry(fn).It returns a wrapper that tries to execute
fnup to three times.Each time an error occurs, we log
[RETRY] Attempt X failed.If the final attempt also fails, we rethrow the error.
If any attempt succeeds, we return immediately.
...