Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 11c73f6

Browse files
Update 12.md
1 parent d5d1f58 commit 11c73f6

File tree

1 file changed

+22
-43
lines changed
  • 12_Asynchronous_Concepts

1 file changed

+22
-43
lines changed

‎12_Asynchronous_Concepts/12.md‎

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,30 @@ 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"); // This will be printed first
2828

2929
functionTwo();
3030

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

3434
const functionTwo = () => {
35-
console.log("Function two"); // 2
35+
console.log("Function two"); // This will be printed second
3636
};
37+
38+
// Asynchronous Code
39+
40+
const functionOne = () => {
41+
console.log("Function One"); // This will be printed first
42+
43+
functionTwo();
44+
45+
console.log("Function One, Part two"); // This will be printed second
46+
};
47+
48+
const functionTwo = () => {
49+
setTimeout(() => console.log("Function two"), 2000); // This will be printed third
50+
3751
```
3852
3953
#### Callback functions
@@ -52,20 +66,20 @@ fetchUser("Shubham", (user) => {
5266
});
5367
```
5468
55-
The two problems that we faced in callbacks are:-
69+
There are two problems that we face in callbacks:-
5670
5771
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.
5872
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.
5973
6074
#### Promises
6175
62-
//In JavaScript, a Promise is an object that represents the result of an asynchronous //operation. A Promise can be in one of three states: pending, fulfilled, or rejected.
76+
In JavaScript, a Promise is an object that represents the result of an asynchronous operation. A Promise can be in one of three states: pending, fulfilled, or rejected.
6377
64-
//A Promise starts in the pending state, and it can either be fulfilled with a value or //rejected with a reason (error). Once a Promise is fulfilled or rejected, it is considered //settled, and it cannot be changed anymore.
78+
A Promise starts in the pending state, and it can either be fulfilled with a value or rejected with a reason (error). Once a Promise is fulfilled or rejected, it is considered settled, and it cannot be changed anymore.
6579
66-
//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.
80+
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.
6781
68-
```js{
82+
```js
6983
// Let's say we have a shopping cart
7084
const cart = ['shoes','pants','shirt'];
7185

@@ -101,38 +115,3 @@ createOrder(cart)
101115
// 1. To avoid Inversion of Control [Not calling the fuction over another function]
102116
// 2. To avoid Call Back hell and have better control of our code
103117
```
104-
105-
```js
106-
const fetchUser = (username) => {
107-
return new Promise((resolve, reject) => {
108-
setTimeout(() => {
109-
console.log("[Now we have the user]");
110-
111-
resolve({ username });
112-
}, 2000);
113-
});
114-
};
115-
116-
const fetchUserPhotos = (username) => {
117-
return new Promise((resolve, reject) => {
118-
setTimeout(() => {
119-
console.log(`Now we have the photos for ${username}`);
120-
resolve(["Photo1", "Photo2"]);
121-
}, 2000);
122-
});
123-
};
124-
125-
const fetchPhotoDetails = (photo) => {
126-
return new Promise((resolve, reject) => {
127-
setTimeout(() => {
128-
console.log(`[Now we have the photo details ${photo}]`);
129-
resolve("details...");
130-
}, 2000);
131-
});
132-
};
133-
134-
fetchUser("Shubham")
135-
.then((user) => fetchUserPhotos(user.username))
136-
.then((photos) => fetchPhotoDetails(photos[0]))
137-
.then((details) => console.log(`Your photo details are ${details}`));
138-
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /