mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
docs: readme changes (#7042)
* doc: remove note about require and intellisense This node about using `require()` appears above an example using ES6 imports, which doesn't follow. I suspect this example was updated and removal of the note was overlooked. * doc: modernize README.md examples with async/await Many of the examples use `.then()` and `.catch()` when an `await` with `try ... catch` would be more concise. This commit does not change all examples in recognition of the fact that sometimes `.then()` and `.catch()` are more sensible depending on the context. --------- Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -199,26 +199,16 @@ Using unpkg CDN:
|
|||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
> **Note**: CommonJS usage
|
|
||||||
> In order to gain the TypeScript typings (for intellisense / autocomplete) while using CommonJS imports with `require()`, use the following approach:
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
//const axios = require('axios'); // legacy way
|
//const axios = require('axios'); // legacy way
|
||||||
|
|
||||||
// Make a request for a user with a given ID
|
try {
|
||||||
axios.get('/user?ID=12345')
|
const response = await axios.get('/user?ID=12345');
|
||||||
.then(function (response) {
|
console.log(response);
|
||||||
// handle success
|
} catch (error) {
|
||||||
console.log(response);
|
console.error(error);
|
||||||
})
|
}
|
||||||
.catch(function (error) {
|
|
||||||
// handle error
|
|
||||||
console.log(error);
|
|
||||||
})
|
|
||||||
.finally(function () {
|
|
||||||
// always executed
|
|
||||||
});
|
|
||||||
|
|
||||||
// Optionally the request above could also be done as
|
// Optionally the request above could also be done as
|
||||||
axios.get('/user', {
|
axios.get('/user', {
|
||||||
@@ -253,16 +243,11 @@ async function getUser() {
|
|||||||
Performing a `POST` request
|
Performing a `POST` request
|
||||||
|
|
||||||
```js
|
```js
|
||||||
axios.post('/user', {
|
const response = await axios.post('/user', {
|
||||||
firstName: 'Fred',
|
firstName: 'Fred',
|
||||||
lastName: 'Flintstone'
|
lastName: 'Flintstone'
|
||||||
})
|
});
|
||||||
.then(function (response) {
|
console.log(response);
|
||||||
console.log(response);
|
|
||||||
})
|
|
||||||
.catch(function (error) {
|
|
||||||
console.log(error);
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Performing multiple concurrent requests
|
Performing multiple concurrent requests
|
||||||
@@ -303,14 +288,12 @@ axios({
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
// GET request for remote image in node.js
|
// GET request for remote image in node.js
|
||||||
axios({
|
const response = await axios({
|
||||||
method: 'get',
|
method: 'get',
|
||||||
url: 'https://bit.ly/2mTM3nY',
|
url: 'https://bit.ly/2mTM3nY',
|
||||||
responseType: 'stream'
|
responseType: 'stream'
|
||||||
})
|
});
|
||||||
.then(function (response) {
|
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
|
||||||
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
##### axios(url[, config])
|
##### axios(url[, config])
|
||||||
@@ -671,14 +654,12 @@ The response for a request contains the following information.
|
|||||||
When using `then`, you will receive the response as follows:
|
When using `then`, you will receive the response as follows:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
axios.get('/user/12345')
|
const response = await axios.get('/user/12345')
|
||||||
.then(function (response) {
|
console.log(response.data);
|
||||||
console.log(response.data);
|
console.log(response.status);
|
||||||
console.log(response.status);
|
console.log(response.statusText);
|
||||||
console.log(response.statusText);
|
console.log(response.headers);
|
||||||
console.log(response.headers);
|
console.log(response.config);
|
||||||
console.log(response.config);
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
When using `catch`, or passing a [rejection callback](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) as second parameter of `then`, the response will be available through the `error` object as explained in the [Handling Errors](#handling-errors) section.
|
When using `catch`, or passing a [rejection callback](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) as second parameter of `then`, the response will be available through the `error` object as explained in the [Handling Errors](#handling-errors) section.
|
||||||
@@ -732,7 +713,8 @@ instance.get('/longRequest', {
|
|||||||
|
|
||||||
## Interceptors
|
## Interceptors
|
||||||
|
|
||||||
You can intercept requests or responses before they are handled by `then` or `catch`.
|
You can intercept requests or responses before methods like `.get()` or `.post()`
|
||||||
|
resolve their promises (before code inside `then` or `catch`, or after `await`)
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user