Skip to content

Commit

Permalink
issue#2609 | Sasha | predictable axios requests
Browse files Browse the repository at this point in the history
- axios requests are not delayed by pre-emptive promise creation by default
- add options to interceptors api ("synchronous" and "runWhen"
- add documentation and unit tests
  • Loading branch information
sashaKorotkov committed Jan 30, 2020
1 parent 7df84a7 commit 6885ac5
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 1,735 deletions.
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -530,6 +530,33 @@ const instance = axios.create();
instance.interceptors.request.use(function () {/*...*/});
```

When you add request interceptors, they are presumed to be asynchronous by default. This can cause a delay
in the execution of your axios request when the main thread is blocked (a promise is created under the hood for
the interceptor and your request gets put on the bottom of the call stack). If your request interceptors are synchronous you can add a flag
to the options object that will tell axios to run the code synchronously and avoid any delays in request execution.

```js
axios.interceptors.request.use(function (config) {
config.headers.test = 'I am only a header!';
return config;
}, null, { synchronous: true });
```

If you want to execute a particular interceptor based on a runtime check,
you can add a `runWhen` function to the options object. The function will be called with the config
object (don't forget that you can bind your own arguments to it as well.) This can be handy when you have an
asynchronous request interceptor that only needs to run at certain times.

```js
function onGetCall(config) {
return config.method === 'get';
}
axios.interceptors.request.use(function (config) {
config.headers.test = 'special get headers';
return config;
}, null, { runWhen: onGetCall });
```

## Handling Errors

```js
Expand Down

0 comments on commit 6885ac5

Please sign in to comment.