Skip to content

Commit 6711048

Browse files
committed
Updated Readme
1 parent 7e52177 commit 6711048

File tree

1 file changed

+36
-34
lines changed
  • 12_Asynchronous_Concepts

1 file changed

+36
-34
lines changed

12_Asynchronous_Concepts/12.md

+36-34
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// clearInterval
88

99
// outputs Hello, world! Every 2 seconds
10-
const myInterval = setInterval(() => console.log("Hello, world!"), 2000);
11-
clearInterval(myInterval); // Clears the Interval
10+
const myInterval = setInterval(() => console.log("Hello, world!"), 2000);
11+
clearInterval(myInterval); // Clears the Interval
1212

1313
// setTimeout
1414
// clearTimeout
@@ -24,17 +24,16 @@ console.log("logging in the bottom");
2424
```js
2525
// Synchronous Code
2626
const functionOne = () => {
27-
console.log("Function One"); // 1
27+
console.log("Function One"); // 1
2828

29-
functionTwo();
29+
functionTwo();
3030

31-
console.log("Function One, Part two"); // 3
31+
console.log("Function One, Part two"); // 3
3232
};
3333

3434
const functionTwo = () => {
35-
console.log("Function two"); // 2
35+
console.log("Function two"); // 2
3636
};
37-
3837
```
3938

4039
#### Callback functions
@@ -43,16 +42,21 @@ const functionTwo = () => {
4342
// Callback functions
4443

4544
const fetchUser = (username, callback) => {
46-
setTimeout(() => {
47-
callback({ username });
48-
}, 2000);
45+
setTimeout(() => {
46+
callback({ username });
47+
}, 2000);
4948
};
5049

5150
fetchUser("Shubham", (user) => {
52-
console.log(`Hello, ${user.username}`); // Hello, Shubham
51+
console.log(`Hello, ${user.username}`); // Hello, Shubham
5352
});
5453
```
5554

55+
The two problems that we faced in callbacks are:-
56+
57+
1. Callback Hell: Asynchronous operations in JavaScript can be achieved through callbacks. Whenever there are multiple dependent Asynchronous operations it will result in a lot of nested callbacks. This will cause a 'pyramid of doom' like structure.
58+
2. Inversion of control: When we give the control of callbacks being called to some other API, this may create a lot of issues. That API may be buggy, may not call our callback and create order as in the above example, may call the payment callback twice etc.
59+
5660
#### Promises
5761

5862
```js
@@ -62,38 +66,36 @@ fetchUser("Shubham", (user) => {
6266

6367
//Promises are used to handle asynchronous operations in a synchronous manner, making it //easier to write and reason about async code. Instead of using callback functions, you can //use the then and catch methods on a Promise to specify what should happen when the Promise //is fulfilled or rejected.
6468

65-
6669
const fetchUser = (username) => {
67-
return new Promise((resolve, reject) => {
68-
setTimeout(() => {
69-
console.log("[Now we have the user]");
70+
return new Promise((resolve, reject) => {
71+
setTimeout(() => {
72+
console.log("[Now we have the user]");
7073

71-
resolve({ username });
72-
}, 2000);
73-
});
74+
resolve({ username });
75+
}, 2000);
76+
});
7477
};
7578

7679
const fetchUserPhotos = (username) => {
77-
return new Promise((resolve, reject) => {
78-
setTimeout(() => {
79-
console.log(`Now we have the photos for ${username}`);
80-
resolve(["Photo1", "Photo2"]);
81-
}, 2000);
82-
});
80+
return new Promise((resolve, reject) => {
81+
setTimeout(() => {
82+
console.log(`Now we have the photos for ${username}`);
83+
resolve(["Photo1", "Photo2"]);
84+
}, 2000);
85+
});
8386
};
8487

8588
const fetchPhotoDetails = (photo) => {
86-
return new Promise((resolve, reject) => {
87-
setTimeout(() => {
88-
console.log(`[Now we have the photo details ${photo}]`);
89-
resolve("details...");
90-
}, 2000);
91-
});
89+
return new Promise((resolve, reject) => {
90+
setTimeout(() => {
91+
console.log(`[Now we have the photo details ${photo}]`);
92+
resolve("details...");
93+
}, 2000);
94+
});
9295
};
9396

9497
fetchUser("Shubham")
95-
.then((user) => fetchUserPhotos(user.username))
96-
.then((photos) => fetchPhotoDetails(photos[0]))
97-
.then((details) => console.log(`Your photo details are ${details}`));
98+
.then((user) => fetchUserPhotos(user.username))
99+
.then((photos) => fetchPhotoDetails(photos[0]))
100+
.then((details) => console.log(`Your photo details are ${details}`));
98101
```
99-

0 commit comments

Comments
 (0)