-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasynchronous_javascript.js
More file actions
191 lines (150 loc) · 4.77 KB
/
asynchronous_javascript.js
File metadata and controls
191 lines (150 loc) · 4.77 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//callback
const sumOfNumber = function (num1, num2, callback1, callback2) {
const result = num1 + num2;
callback1(result, callback2);
}
const sumOfNumbe = function (num1, num2, callback1, callback2) {
const result = num1 + num2;
callback1(result, callback2);
}
// const add = (n1, n2, (n1, n2) => {
// return n1 + n2;
// })
// console.log(add(3, 4))
// sumOfNumber(2,5,function(result){
// console.log("this is the answer provide after result generate by callback ")
// console.log(result)
// })
const cb1 = (res, cb2) => {
cb2(res * res);
}
const cb2 = (ans) => {
console.log(`this is the answer to convert into square ${ans}`)
}
// sumOfNumber(3,4,cb1,cb2);
// const sumOfNumber = (function(res,cb1){
// res = num1 + num2;
// cb1(res);
// },num1,num2)
// console.log(sumOfNumber(2,4))
// )=>{
// res = num1+num2;
// }
// =>{
// // cb1(num1 + num2);
// }
//promise
// The Promise object represents the eventual completion(or failure) of an
// asynchronous operation and its resulting value.
// **important
// --> promise is the imuutable object that means not change the object data
// in promise three states accoding its execute by follow this states
// pending: initial state, neither fulfilled nor rejected.
// fulfilled: meaning that the operation was completed successfully.
// rejected: meaning that the operation failed.
//let's create own promise
// here example that we make the card with its orderid when order procced than according order-api
// we take orderid and vaildate if its true than promise resolve and than we procced to payment with its
// order-id
let validateCard = (orderid) => {
return orderid === "" ? false : true;
}
let successfullyPaymentCard = (orderid) => {
// here we create the promise object which contain the resolve and reject callback
const pr = new Promise(function (resolve, rejected) {
if (!validateCard(orderid)) {
const err = new Error("this is the invalidate the orderid / product");
rejected(err);
} else {
setTimeout(() => {
resolve(orderid);
}, 3000);
}
})
return pr;
}
// const promise = successfullyPaymentCard("234")
// promise chain
promise
.then((orderId) => {
console.log(orderId); //when promise resolve its called and its return orderid as promise
return orderId; // return orderid as promise
})
.then((orderId) => {
//this logic here its say when you not return than return undefinded after specific time its return the orderid
// after two second
// setTimeout(() => {
// console.log(orderId);
// return orderId;
// }, 2000);
return orderId //here return orderid but above settimeout which timer 2s therefore after 2s above code execute when promise chain resolve (all of the code executed)
})
.then((orderId) => {
setTimeout(() => {promise.then((orderid) => {
console.log("product orderid : ", orderid)
}).catch((err) => {
console.log(err.message);
})
let cb3 = function () {
setTimeout(() => {
console.log("this is the execute after 1 second ")
}, 3000);
}
cb3();
console.log(orderId)
}, 2000);
// return orderId;
})
.then((orderId) => {
//the chain arrived to here after five second and its get the undefined
console.log(orderId);
return orderId;
})
.catch((err) => {
console.log(err.message);
});
// when any perticular time settimeout and its promise both not called at time more priority
// promise execute due to micro-task queue execute first
// after that callback queue content execute
//all the things mange by the event loop
promise.then((orderid) => {
console.log("product orderid : ", orderid)
}).catch((err) => {
console.log(err.message);
})
let cb3 = function () {
setTimeout(() => {
console.log("this is the execute after 1 second ")
}, 3000);
}
cb3();
//async-await
// There’s a special syntax to work with promises in a more comfortable fashion, called “async/await”.
// It’s surprisingly easy to understand and use.
async function f() {
// return 1;
// you can also same as
return Promise.resolve(1);
}
f().then((e) => console.log(e)); //return the promise
// await, that works only inside async functions
async function fun() {
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("done!"), 1000)
});
let result = await promise; // wait until the promise resolves (*)
// console.log(result)
return result
}
// one method to use with then and catch
// fun().then((e)=>{
// console.log(e);
// }).catch((err)=>{
// console.log(err);
// })
// console.log(fun())
const res = async()=>{
const re = await fun();
console.log(re);
}
res();