-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.2.promises.js
More file actions
101 lines (81 loc) · 3.4 KB
/
17.2.promises.js
File metadata and controls
101 lines (81 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Promises are used to handle async operations in javascript
// promise is an object that represents the eventual completion or failure of the asynchronous operation
// Suppose in an ecommerrce website you've added some items in the cart
// const cart=["shoes","pants","kurta"];
// 2 API's | Both the API's are asynchronous in nature but they are dependent on each other ie the response of the 1st API will be used by the 2nd API
// createOrder(cart); // It will generate a specific order Id
// proceedToPayment(orderId);
// Lets see how we can write code using callbacks
// we will wrap this API inside the callback function and pass it as an argument in the createOrder API
// createOrder(cart,callback_function);
// createOrder(cart,function(){
// proceedToPayment(orderId);
// });
//Here are passing the callback function to the createOrder API and blindly trusting over it
// In this way we handle asynchronous operations using callbacks
// This promise in an empty object with some data value in it ie {data:(API_returned_data)}
// const promise=createOrder(cart);
// whenever the JS engine will execute this line it will create an empty object with some data value in it ie {data:undefined} and at some later stage when the API is called it will fill this value {data:orderDetails}
// Here we are attaching the callback function to this promise object using then function
// promise.then(function(orderId){
// proceedToPayment(orderId);
// }) //This will be called whenever their is data inside the promise object
// Here this function will be called once
// var settings={
// "url":"api endpoint",
// "method":"GET",
// "data":JSON.stringify(obj),
// };
// $ajax(settings).done(function(response){
// console.log(response);
// })
// Lets use the fetch function to call
const URL="https://restcountries.com/v3.1/all"
// fetch() function is basically an API given by browser to us to make external calls[http requests]
const user= fetch(URL); // This fetch function will return me the promise object
// Lets observe the running state of fetch API
/*
user-> {
[[Prototype]]:Promise,
[[Promise State]]:"pending", // Have 3 states "pending" , "fulfilled" and "rejected"
[[PromiseResult]]:undefined
}
*/
console.log(user); // At this point of time the promise object is in the pending state
user.then((response)=>{
return response.json();
})
.then((data)=>{
console.log(data);
})
.catch((error)=>{
console.log("Error fetching the data:",error);
})
/*
This nesting can lead to the callback hell
createOrder(cart,function(orderId){
proceedToPayment(orderId,function(paymentInfo){
showOrderSummary(paymentInfo,function(){
updateWalleteBalence();
})
})
})
But with the help of promises
// NOTE : MUST WRITE THE RETURN STATEMENT FOR THE PROPER IN FLOW OF DATA...................
const promise=createOrder(cart); OR
createOrder(cart) // Jo bhi createOrder ka result aayga vo pass hoga orderId vala param main
.then(function(orderId){
return proceedToPayment(orderId); // Iska result agli api ke param main pass hoga
})
.then(function(paymentInfo){
return showOrderSummary(paymentInfo);
})
.then(function(){
return updateWalletBalance();
})
Using arrow functions
createOrder(cart)
.then(cart=>proceedToPayment(orderId))
.then(paymentInfo=>showOrderSummary(paymentInfo))
.then(paymentInfo=>updateWalletBalance(paymentInfo))
*/