Skip to content

Commit 9d997e0

Browse files
Add axios.js
1 parent 427bc12 commit 9d997e0

3 files changed

Lines changed: 318 additions & 0 deletions

File tree

part9_Advanced/axios.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// INFO: Making a Basic Request
2+
const axios = require('axios');
3+
4+
axios.get('https://jsonplaceholder.typicode.com/posts')
5+
.then(res => console.log(res.data))
6+
.catch(error => console.error('Error:', error));
7+
8+
/*
9+
1). Axios automatically parses json responses
10+
2). It handles errors better by rejecting the Promise for non-2xx status codes.
11+
*/
12+
13+
14+
// INFO: USing async await wth axios
15+
async function fetchData() {
16+
try {
17+
const response = await axios.get('https://jsonplaceholder.typicode.com/posts');
18+
console.log(response.data);
19+
} catch (error) {
20+
console.error('Error:', error);
21+
}
22+
}
23+
fetchData();

part9_Advanced/package-lock.json

Lines changed: 290 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

part9_Advanced/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"axios": "^1.8.3"
4+
}
5+
}

0 commit comments

Comments
 (0)