You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//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.
60
+
61
+
//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.
62
+
63
+
//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.
64
+
65
+
66
+
constfetchUser= (username) => {
67
+
returnnewPromise((resolve, reject) => {
68
+
setTimeout(() => {
69
+
console.log("[Now we have the user]");
70
+
71
+
resolve({ username });
72
+
}, 2000);
73
+
});
74
+
};
75
+
76
+
constfetchUserPhotos= (username) => {
77
+
returnnewPromise((resolve, reject) => {
78
+
setTimeout(() => {
79
+
console.log(`Now we have the photos for ${username}`);
80
+
resolve(["Photo1", "Photo2"]);
81
+
}, 2000);
82
+
});
83
+
};
84
+
85
+
constfetchPhotoDetails= (photo) => {
86
+
returnnewPromise((resolve, reject) => {
87
+
setTimeout(() => {
88
+
console.log(`[Now we have the photo details ${photo}]`);
89
+
resolve("details...");
90
+
}, 2000);
91
+
});
92
+
};
93
+
94
+
fetchUser("Shubham")
95
+
.then((user) =>fetchUserPhotos(user.username))
96
+
.then((photos) =>fetchPhotoDetails(photos[0]))
97
+
.then((details) =>console.log(`Your photo details are ${details}`));
0 commit comments