Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Updated] Document options when adding interceptors #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions posts/en/interceptors.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ next_title: 'Handling Errors'
next_link: '/docs/handling_errors'
---

You can intercept requests or responses before they are handled by `then` or `catch`.
You can intercept requests or responses **before** they are handled by `then` or `catch`.

The `use` function adds a handler to the list of handlers to be run when the Promise is fulfilled or rejected. The handler is defined by the fulfilled and rejected functions.

There is an optional `options` object that can be passed in as the third parameter. `synchronous` if the synchronous option is true. The handler is defined as asynchronous if the synchronous option is false. If the synchronous option is not provided, the handler is defined as asynchronous. `runWhen` will control when the provided interceptor will run. Provide a function that will return true or false on whether it should run, defaults to always true.

```js
// Add a request interceptor
Expand All @@ -16,7 +20,9 @@ axios.interceptors.request.use(function (config) {
}, function (error) {
// Do something with request error
return Promise.reject(error);
});
},
{ synchronous: true, runWhen: () => /* This function returns true */}
);

// Add a response interceptor
axios.interceptors.response.use(function (response) {
Expand Down