Skip to content

Commit e092b5e

Browse files
feat: add async await
1 parent 91068ec commit e092b5e

File tree

1 file changed

+49
-56
lines changed

1 file changed

+49
-56
lines changed

part9 (Advanced Topics)/06_async_await.js

Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,93 @@
11
// NOTE: An aysnc function is a special type of function that always returns a Promise. It allows to write asynchronous code in a cleaner, more readable way using await intead of .then() chains
22

3-
43
async function makeRequest() {
5-
console.log('helo');
6-
throw new Error('error in program');
4+
console.log("helo");
5+
throw new Error("error in program");
76
}
87
makeRequest();
98

10-
119
async function newRequest() {
12-
const url = 'https://dummyjson.com/products'
13-
const responsePromise = fetch(url)
14-
responsePromise.then((res) => {
15-
res.json().then((d) => {
16-
console.log(d);
17-
})
18-
})
10+
const url = "https://dummyjson.com/products";
11+
const responsePromise = fetch(url);
12+
responsePromise.then((res) => {
13+
res.json().then((d) => {
14+
console.log(d);
15+
});
16+
});
1917
}
2018
newRequest();
2119

22-
23-
2420
async function request() {
25-
const url = 'https://dummyjson.com/products';
26-
const response = await fetch(url) // await returns the response of the fetched url means the result
27-
const data = await response.json()
28-
console.log(data);
21+
const url = "https://dummyjson.com/products";
22+
const response = await fetch(url); // await returns the response of the fetched url means the result
23+
const data = await response.json();
24+
console.log(data);
2925
}
3026
request();
3127

32-
33-
3428
// Best way to handle api using async function
3529
async function fetchData(url) {
36-
try {
37-
let response = await fetch(url);
38-
let data = await response.json();
39-
console.log(data);
40-
} catch (error) {
41-
console.error("Error:", error);
42-
}
30+
try {
31+
let response = await fetch(url);
32+
let data = await response.json();
33+
console.log(data);
34+
} catch (error) {
35+
console.error("Error:", error);
36+
}
4337
}
4438

4539
fetchData("https://fakestoreapi.com/products");
4640

47-
4841
// THis one is also the best way to use async & await
4942
async function getProducts() {
50-
try {
51-
const url = 'https://dummyjson.com/products';
52-
const res = await fetch(url);
53-
54-
if (!res.ok) {
55-
throw new Error(`HTTP error! Status: ${res.status}`);
56-
}
43+
try {
44+
const url = "https://dummyjson.com/products";
45+
const res = await fetch(url);
5746

58-
const data = await res.json();
59-
console.log(data);
60-
} catch (error) {
61-
console.error("Error fetching products:", error.message);
47+
if (!res.ok) {
48+
throw new Error(`HTTP error! Status: ${res.status}`);
6249
}
50+
51+
const data = await res.json();
52+
console.log(data);
53+
} catch (error) {
54+
console.error("Error fetching products:", error.message);
55+
}
6356
}
6457
getProducts();
6558

6659
async function getCarts() {
67-
try {
68-
const url = 'https://dummyjson.com/carts';
69-
const res = await fetch(url);
70-
if (!res.ok) {
71-
throw new Error(`Http error! Statu: ${res.status}`);
72-
}
73-
const data = await res.json();
74-
console.log(data);
75-
} catch (error) {
76-
console.log("Error Feching carts", error.message);
60+
try {
61+
const url = "https://dummyjson.com/carts";
62+
const res = await fetch(url);
63+
if (!res.ok) {
64+
throw new Error(`Http error! Statu: ${res.status}`);
7765
}
66+
const data = await res.json();
67+
console.log(data);
68+
} catch (error) {
69+
console.log("Error Feching carts", error.message);
70+
}
7871
}
7972
getCarts();
8073

81-
8274
// NOTE: Async await Quizes
8375

8476
// INFO: First quiz
8577
async function test() {
86-
return "Hello";
78+
return "Hello";
8779
}
8880
console.log(test()); // Answer : Promise { <fulfilled>: "Hello" }
8981

90-
9182
// INFO: Second Quiz
9283
async function fetchData() {
93-
console.log("start");
94-
let promise = new Promise(resolve => setTimeout(() => resolve("Data loaded"), 2000));
95-
console.log("waiting....");
96-
let data = await promise;
97-
console.log(data);
84+
console.log("start");
85+
let promise = new Promise((resolve) =>
86+
setTimeout(() => resolve("Data loaded"), 2000),
87+
);
88+
console.log("waiting....");
89+
let data = await promise;
90+
console.log(data);
9891
}
9992

10093
fetchData();

0 commit comments

Comments
 (0)