Q1: Write one example explaining how you can write a callback function.
18 |ANS :-
19 |
20 | function calculate(num1, num2){
21 | return num1 * num2
22 | }
23 |
24 | function displayDate(name, age){
25 | console.log(`${name} : age is ${age}`)
26 | }
27 |
28 | displayDate("Nitin", calculate(19,1));
29 |
30 | OUTPUT :-
31 |
33 |
Q2: Write a callback function to print numbers from 1 to 7, in which 1 should be printed after 1 sec, 2 should be printed after 2 sec, 3 should be printed after 3 sec, and so on.
35 |
36 | Explain callback hell.
37 |
38 | Numbers
39 |
40 | 1
41 | 2
42 | 3
43 | 4
44 | 5
45 | 6
46 | 7
47 |
ANS :-
49 |
50 | setTimeout(()=>{
51 | console.log("1")
52 | setTimeout(()=>{
53 | console.log("2")
54 | setTimeout(()=>{
55 | console.log("3");
56 | setTimeout(()=>{
57 | console.log("4");
58 | setTimeout(()=>{
59 | console.log("5");
60 | setTimeout(()=>{
61 | console.log("6");
62 | setTimeout(()=>{
63 | console.log("7")
64 | },7000)
65 | },6000)
66 | },5000)
67 | },4000)
68 | },3000)
69 | },2000)
70 | },1000)
71 |
72 | OUTPUT :-
73 |75 |
Q1: "Write the promise function to print numbers from 1 to 7, in which 1 should be printed after 1 sec, 2 should be printed after 2 sec, 3 should be printed after 3 sec, and so on.
77 |
78 | Numbers
79 | 1
80 | 2
81 | 3
82 | 4
83 | 5
84 | 6
85 | 7
86 |
88 | let ans3 = document.getElementById("ans3")
89 | function promiseChain(alpha, timeout){
90 | return new Promise((res, rej)=>{
91 | setTimeout(()=>{
92 | console.log(alpha);
93 | res("This promise is resolved !!!....")
94 | },timeout);
95 | })
96 | }
97 | function visible(){
98 | promiseChain("1",1000)
99 | .then(()=>promiseChain("2", 2000))
100 | .then(()=>promiseChain("3", 3000))
101 | .then(()=>promiseChain("4", 4000))
102 | .then(()=>promiseChain("5", 5000))
103 | .then(()=>promiseChain("6", 6000))
104 | .then(()=>promiseChain("7", 7000))
105 | }
106 | visible();
107 |
108 | OUTPUT :-
109 |111 |
Q4: Create a promise function accepting an argument, if yes is passed to the function then it should go to resolved state and print Promise Resolved, and if nothing is passed 113 | then it should go to reject the state and catch the error and print Promise Rejected 114 |
115 |ANS :-
116 |
117 | let variable = true
118 | function promiseFun(){
119 | return new Promise((resolve,reject)=>{
120 | if(variable === true){
121 | resolve("Promise resolve succesfully....")
122 | }
123 | else{
124 | reject(Error("Promise rejected !!!"))
125 | }
126 | })
127 | }
128 |
129 | promiseFun(variable).then((date)=>{
130 | console.log(date)
131 | }).catch((err)=>{
132 | console.log(err)
133 | })
134 |
135 | OUTPUT :-
136 |138 |
Q5: Create examples to explain callback function
140 |ANS :-
141 |#A callback function is a function passed to another function as an argument. Callbacks promote asynchronous programming
142 |
143 |
144 |
145 | function divide(num1, num2, callback) {
146 | let output = num1 / num2;
147 | callback(output);
148 | }
149 |
150 | function division(output) {
151 | console.log('The result is: ' + output);
152 | }
153 |
154 | divide(100, 100, division);
155 |
156 | OUTPUT :-
157 |159 |
Q6: Create examples to explain callback hell function
161 |ANS:-
162 |The phenomenon which happens when we nest multiple callbacks within a function is called a callback hell. The shape of the resulting code structure resembles a pyramid and hence callback hell is also called the “pyramid of the doom”. It makes the code very difficult to understand and maintain.
163 |
164 |
165 |
166 |
167 | setTimeout(()=>{
168 | console.log("1")
169 | setTimeout(()=>{
170 | console.log("2")
171 | setTimeout(()=>{
172 | console.log("3");
173 | setTimeout(()=>{
174 | console.log("4");
175 | setTimeout(()=>{
176 | console.log("5");
177 | setTimeout(()=>{
178 | console.log("6");
179 | setTimeout(()=>{
180 | console.log("7")
181 | },7000)
182 | },6000)
183 | },5000)
184 | },4000)
185 | },3000)
186 | },2000)
187 | },1000)
188 |
189 | OUTPUT :-
190 |192 |
Q7 :Create examples to explain promises function
194 |ANS :-
195 |Promise Function are the functions that return a promise object. A promise is an object which represents the completion or failure of an asynchronous operation 196 | along with its result. They come with in-built error handling. Here inside the findSum() function, promise object "pr" has been created. 197 | Then the function is called and ".then" block is attached to handle the resolved part and ".catch" handles the reject part.
198 |
199 |
200 |
201 | function findSum(num1,num2){
202 | let pr = new Promise(function(res,rej){
203 | setTimeout(function(){
204 | let sum = num1 + num2;
205 | if(isNaN(sum))
206 | {
207 | rej("Try Again! Enter a valid number");
208 | }
209 | else
210 | {
211 | res(sum);
212 | }
213 | }, 2000)
214 | })
215 | return pr;
216 | }
217 |
218 | findSum(26,23).then(function(ans){
219 | console.log("Sum =", ans);
220 | }).catch(function(err){
221 | console.log(err);
222 | })
223 |
224 | OUTPUT :-
225 |227 |
Q8: Create examples to explain async await function
229 |ANS :-
230 |
231 | function fetchData() {
232 | return new Promise(resolve => {
233 | setTimeout(() => resolve('Data received!'), 2000);
234 | });
235 | }
236 |
237 | async function printData() {
238 | console.log("Fetching data...");
239 | const data = await fetchData();
240 | console.log(data);
241 | }
242 |
243 | printData();
244 |
245 | OUTPUT :-
246 |248 |
Q9: Create examples to explain promise.all function
250 |ANS :-
251 |
252 | function mypromise(num ){
253 | return new Promise((resolve, reject)=>{
254 | if(num%2 === 0){
255 | resolve("This is even")
256 | }
257 | else{
258 | rej("This is Odd")
259 | }
260 | })
261 | }
262 |
263 | const mypromise1 = new Promise((resolve, reject)=>{
264 | if(true){
265 | resolve("hi EAC _01")
266 | }
267 | else{
268 | reject("Soryy !!!!")
269 | }
270 | })
271 |
272 | Promise.all([mypromise(20), mypromise1]).then((data)=>{
273 | console.log(data)
274 | }).catch((data)=>{
275 | console.log(data)
276 | })
277 |
278 | OUTPUT :-
279 |