> ## Documentation Index
> Fetch the complete documentation index at: https://docs.requestly.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Modifying response asynchronously

Before going into Async Modifications, lets learn about Requestly Interceptor Response Rule a little bit.

[Requestly Interceptor Response Rule](https://developers.requestly.com/http-rules/modify-response-body/) comes in handy when you have to modify the response of a Request. You can either modify the response using `Static Data` or `Programmatically` write a script to modify the response.

## **Programmatic Response Rule**

Modifying Response body using Programmatic mode is really powerful. You can modify the response body programmatically based on incoming request's headers, status code, request method etc.

For example, this rule changes the response body to `{"foo": "bar"}` when request method is `GET` and return original response for rest of the methods.

```bash theme={null}
function modifyResponse(args) {
  const {method, url, response, responseType, requestHeaders, requestData, responseJSON} = args;
  // Change response below depending upon request attributes received in args
  
  if(method === "GET") {
    return {"foo": "bar"}
  }

  return response;
}
```

## **Async Modifications**

Async modifications lets you perform some long running tasks before returning the response. You can learn about async await from [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function). This can be helpful in the following scenarios:

## Usecases

* [Changing response based on response from some other URL](https://developers.requestly.com/guides/async-response-modification/#block-0da2a5daea2b462792cdad3899bf8099).

- [Respond to the request with the response from another website](https://developers.requestly.com/guides/async-response-modification/#block-add97b322e7340499f2766bb9ff41345).

* Adding delays in Response (This is also possible using [Requestly Interceptor Delay Rule](https://developers.requestly.com/http-rules/delay-request/))

### **Example 1**

In this example, we'll try to hit an external URL to fetch the auth\_token and then add the token in the original response.

> Rule: [https://app.requestly.io/rules#sharedList/1677765397812-Async-Modification-Response-Rule](https://app.requestly.io/rules#sharedList/1677765397812-Async-Modification-Response-Rule)

<img src="https://mintcdn.com/requestly/7MWqoAA3Dhf9dCCV/images/modifying-response-asynchronously/891ff9d6-50dd-4972-833b-f46130798ccd.png?fit=max&auto=format&n=7MWqoAA3Dhf9dCCV&q=85&s=5f05a13826178453389907dd585334ff" align="left" fullwidth="false" width="2400" height="1408" data-path="images/modifying-response-asynchronously/891ff9d6-50dd-4972-833b-f46130798ccd.png" />

<img src="https://mintcdn.com/requestly/7MWqoAA3Dhf9dCCV/images/modifying-response-asynchronously/08d5df69-abf3-47e2-8a3a-4ca6c331fca8.png?fit=max&auto=format&n=7MWqoAA3Dhf9dCCV&q=85&s=4c4f352175cb07b2a41c248fd32feaa8" align="left" fullwidth="false" width="2400" height="782" data-path="images/modifying-response-asynchronously/08d5df69-abf3-47e2-8a3a-4ca6c331fca8.png" />

Original Response

<img src="https://mintcdn.com/requestly/eSOeLVkmwVPWjPJw/images/modifying-response-asynchronously/c46cb45c-41b9-47f0-b792-50b7d33af8de.png?fit=max&auto=format&n=eSOeLVkmwVPWjPJw&q=85&s=e019e27a14dd6f947777a506c083a71e" align="left" fullwidth="false" width="2400" height="672" data-path="images/modifying-response-asynchronously/c46cb45c-41b9-47f0-b792-50b7d33af8de.png" />

Updated Response with token

1. Fetching Token from URL `https://demo_requestly.requestly.dev/auth_token`.

2) Adding token in Response.

### Example 2

This example changes the response of a non-existent domain ([https://non-existent-url.com](https://non-existent-url.com/)) by fetching the response from another url ([https://demo\_requestly.requestly.dev/users/1](https://demo_requestly.requestly.dev/users/1))

> Rule: [https://app.requestly.io/rules#sharedList/1677766334864-Async-Modification-Changing-response-of-non-existent-domain-programmatically](https://app.requestly.io/rules#sharedList/1677766334864-Async-Modification-Changing-response-of-non-existent-domain-programmatically)

<img src="https://mintcdn.com/requestly/7MWqoAA3Dhf9dCCV/images/modifying-response-asynchronously/7ba815cc-7867-4ffb-9c55-de4009c1ce84.png?fit=max&auto=format&n=7MWqoAA3Dhf9dCCV&q=85&s=e9aa9eea94e053194168ef70fa66a3b0" align="left" fullwidth="false" width="2400" height="1416" data-path="images/modifying-response-asynchronously/7ba815cc-7867-4ffb-9c55-de4009c1ce84.png" />

<img src="https://mintcdn.com/requestly/7MWqoAA3Dhf9dCCV/images/modifying-response-asynchronously/8de0b446-831c-4acc-8973-f2ca15b219a0.png?fit=max&auto=format&n=7MWqoAA3Dhf9dCCV&q=85&s=24adaf1c43c06c12c3fb51ee9ea3cca9" align="left" fullwidth="false" width="2400" height="738" data-path="images/modifying-response-asynchronously/8de0b446-831c-4acc-8973-f2ca15b219a0.png" />

Original Response

<img src="https://mintcdn.com/requestly/7MWqoAA3Dhf9dCCV/images/modifying-response-asynchronously/b74550f8-1490-4788-9368-37d2001471a0.png?fit=max&auto=format&n=7MWqoAA3Dhf9dCCV&q=85&s=c3d232de4d194715dcfdbb883ba4bfa3" align="left" fullwidth="false" width="2400" height="774" data-path="images/modifying-response-asynchronously/b74550f8-1490-4788-9368-37d2001471a0.png" />

Updated Response
