# Retrying API with HTTP status code 202

There may be a case when you want the app to keep sending the same request until the result is ready. For example, you may want the API to keep returning *202* status code until the data is ready, and then return a *200* with all the data.

Below is the approach we suggest to implement this kind of logic:

1. Create a new action of the **HTTP Request** type (called here *apiCall*):
   1. Specify the *method* and *URL.*
   2. Select the **Transform result** toggle and in the *Modify the result* field add `return {{res}};`.\
      This will return the status of the API request as, by default, the HTTP step returns the body of the response.

<figure><img src="/files/GHEEJsUbXl6Ep5Zy4mYw" alt=""><figcaption></figcaption></figure>

2. Create another action of the **JavaScript Code** type (called here *callApiWithRetries*) and specify the following code:

```javascript
async function makeApiCallWithRetry(maxRetries = 10, delay = 2000) {
  let retries = 0;

  while (retries < maxRetries) {
    const response = await {{actions.apiCall.trigger()}}

    // If the response is 200, return the data
    if (response.status === 200) {
      return response.body;
    }

    // If the response is 202, retry after a delay
    if (response.status === 202) {
      retries++;
      await new Promise(res => setTimeout(res, delay));
    } else {
      throw new Error(`Unexpected status code: ${response.status}`);
    }
  }
}
return await makeApiCallWithRetry();
```

<figure><img src="/files/MIuigNa2MFMn69awyVm2" alt=""><figcaption></figcaption></figure>

Here, the action is called programmatically with `actions.apiCall.trigger()`, which allows you to easier control retry logic.\
But you can also make this retry action generic and execute it providing *actionName* via params. To do so, you simply need to change the code to `actions[params.actionName].trigger()`:

```javascript
actions.apiCall.trigger() -> actions[params.actionName].trigger()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.uibakery.io/how-tos/custom-code/retrying-api-with-http-status-code-202.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
