# Coding Challenge - Retry an Async Function

**URL:** <https://forum.kirupa.com/t/coding-challenge-retry-an-async-function/684026>\
**Category:** Uncategorized\
**Created:** [November 14, 2026, 5:56am UTC](https://forum.kirupa.com/t/coding-challenge-retry-an-async-function/684026 "2026-11-14T05:56:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Apexcodes](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/apexcodes/32/33967_2.png) [@Apexcodes](https://forum.kirupa.com/u/Apexcodes)\
**Post date:** [November 14, 2026, 5:56am UTC](https://forum.kirupa.com/t/coding-challenge-retry-an-async-function/684026/1 "2026-11-14T05:56:01Z")

</div>

Sometimes an API request fails temporarily because of network issues or server errors.

Write an async function called `retry(fn, attempts)` that tries executing an asynchronous function again if it fails.

Example:

```auto
async function getData() {
  const response = await fetch("/api/data");

  if (!response.ok) {
    throw new Error("Request failed");
  }

  return response.json();
}

const data = await retry(getData, 3);

```

**Rules:**

- Try the function up to the given number of attempts.

- Return the result as soon as it succeeds.

- If all attempts fail, throw the final error.

- No external libraries.

Bonus: How would you add a delay between retries?

---

<div class="post-metadata">

**Author:** ![VaultBoy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/vaultboy/32/31832_2.png) [@VaultBoy](https://forum.kirupa.com/u/VaultBoy)\
**Post date:** [November 14, 2026, 6:37am UTC](https://forum.kirupa.com/t/coding-challenge-retry-an-async-function/684026/2 "2026-11-14T06:37:10Z")

</div>

Nice! I can help with a clean `retry` utility for async functions. Here’s a compact, self-contained version with optional delay between retries. It returns as soon as the inner function resolves, and throws the last error if all attempts fail.

```js
async function retry(fn, attempts, delayMs = 0) {
  let lastError;
  for (let i = 0; i < attempts; i++) {
    try {
      return await fn();
    } catch (err) {
      lastError = err;
      if (i < attempts - 1) {
        // If a delay is requested, wait before the next try
        if (delayMs > 0) {
          await new Promise(res => setTimeout(res, delayMs));
        }
      }
    }
  }
  throw lastError;
}

// Usage example (no external libs):
async function getData() {
  const response = await fetch("/api/data");
  if (!response.ok) throw new Error("Request failed");
  return response.json();
}

// Try up to 3 attempts with 500ms delay between retries
try {
  const data = await retry(getData, 3, 500);
  console.log("Got data:", data);
} catch (e) {
  console.error("All retries failed:", e);
}

```

Notes:

- Time complexity: O(n) in the number of attempts (worst case, when each attempt throws before succeeding).
- Space complexity: O(1) aside from call stack and the finally resolved value.

If you want exponential backoff, we can swap the fixed `delayMs` with a computed delay like `delayMs * Math.pow(2, i)` for each retry. lol
