From d8bbebf4a4fe3c9e2090b3574156abbd22fdf09a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Dub=C3=A9?= <7225168+KernelDeimos@users.noreply.github.com> Date: Thu, 6 Nov 2025 13:39:03 -0500 Subject: [PATCH] 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 --- README.md | 62 ++++++++++++++++++++----------------------------------- 1 file changed, 22 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 298fcb2..5e08671 100644 --- a/README.md +++ b/README.md @@ -199,26 +199,16 @@ Using unpkg CDN: ## 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 import axios from 'axios'; //const axios = require('axios'); // legacy way -// Make a request for a user with a given ID -axios.get('/user?ID=12345') - .then(function (response) { - // handle success - console.log(response); - }) - .catch(function (error) { - // handle error - console.log(error); - }) - .finally(function () { - // always executed - }); +try { + const response = await axios.get('/user?ID=12345'); + console.log(response); +} catch (error) { + console.error(error); +} // Optionally the request above could also be done as axios.get('/user', { @@ -253,16 +243,11 @@ async function getUser() { Performing a `POST` request ```js -axios.post('/user', { - firstName: 'Fred', - lastName: 'Flintstone' - }) - .then(function (response) { - console.log(response); - }) - .catch(function (error) { - console.log(error); - }); +const response = await axios.post('/user', { + firstName: 'Fred', + lastName: 'Flintstone' +}); +console.log(response); ``` Performing multiple concurrent requests @@ -303,14 +288,12 @@ axios({ ```js // GET request for remote image in node.js -axios({ +const response = await axios({ method: 'get', url: 'https://bit.ly/2mTM3nY', responseType: 'stream' -}) - .then(function (response) { - response.data.pipe(fs.createWriteStream('ada_lovelace.jpg')) - }); +}); +response.data.pipe(fs.createWriteStream('ada_lovelace.jpg')) ``` ##### 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: ```js -axios.get('/user/12345') - .then(function (response) { - console.log(response.data); - console.log(response.status); - console.log(response.statusText); - console.log(response.headers); - console.log(response.config); - }); +const response = await axios.get('/user/12345') +console.log(response.data); +console.log(response.status); +console.log(response.statusText); +console.log(response.headers); +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. @@ -732,7 +713,8 @@ instance.get('/longRequest', { ## 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