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
## JavaScript interview code challenges on Asynchronous programming - concepts
6
-
7
-
1.[Show the execution of 3 asynchronous block of code, one after the other in sequence](#Q1)
8
-
1.[Write a code to make xmlHTTPRequest to get data from the server asynchronously](#Q2)
9
-
1.[Show the working of promise along with resolve & reject code](#Q3)
10
-
1.[Wrap the setTimeout function to convert to a promise](#Q4)
11
-
1.[Convert the xmlHTTPRequest to promise based function to get the data from the server asynchronously (fetch)](#Q5)
12
-
1.[Make a fetch request to retrieve and store JSON data from server](#Q6)
13
-
1.[Cancel a fetch request](#Q7)
14
-
1.[Show the working of async await work with promises](#Q8)
15
-
1.[Write a code to resolve all the list of asynchronous executions of promises and stop if any of them is rejected. Print the output accordingly](#Q9)
16
-
1.[Write a code to resolve all the list of asynchronous executions of promises no matter if each execution succeeds or fails. Print the output of each](#Q10)
17
-
1.[Explain the working of Promise.race with few asynchronous function example](#Q11)
18
-
1.[Show me the working of a generator function](#Q12)
19
-
1.[Write a generator function which uses another generator function internally to fetch the values. Use for..of loop to consume the values](#Q13)
20
-
1.[Write an interface to mock Promise in JavaScript which can be called to create a promise with resolve and reject. Also implement then functionality](#Q14)
21
-
1.[Write a function which helps to achieve the `Promise.all` functionality using promises](#Q15)
22
-
1.[Show the working generator function with promises](#Q16)
23
-
24
-
---
25
-
26
-
#### Q1
27
-
### Show the execution of 3 asynchronous block of code, one after the other in sequence
5
+
<h2align="center">JavaScript challenges on Asynchronous programming - concepts</h2>
6
+
7
+
<br>
8
+
9
+
### Q. Show the execution of 3 asynchronous block of code, one after the other in sequence
28
10
29
11
- The asynchronous block of code can be a function which executes asynchronously
30
12
- The execution of such function can be simulated using setTimeout to with delay and execute different blocks of code inside each
@@ -58,8 +40,7 @@ The nested blocks of statements shown in the comments which get executed one aft
58
40
59
41
<br />
60
42
61
-
#### Q2
62
-
### Write a code to make xmlHTTPRequest to get data from the server asynchronously
43
+
### Q. Write a code to make xmlHTTPRequest to get data from the server asynchronously
63
44
64
45
- XMLHttpRequest (XHR) objects are used to interact with server to retrieve data from a URL without having to do a full page refresh
65
46
- XHR requests can be initiated by creating the object and providing the arguments such as 'method', url etc
@@ -88,8 +69,7 @@ XHR is used mainly in AJAX programming
88
69
89
70
<br />
90
71
91
-
#### Q3
92
-
### Show the working of promise along with resolve & reject code
72
+
### Q. Show the working of promise along with resolve & reject code
93
73
94
74
- The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value
95
75
- Promise returns an object which ramains in pending state until it is resolved or rejected
@@ -132,8 +112,7 @@ Once the promise is resolved or rejected, status will not change
132
112
133
113
<br />
134
114
135
-
#### Q4
136
-
### Wrap the setTimeout function to convert to a promise
115
+
### Q. Wrap the setTimeout function to convert to a promise
137
116
138
117
- Promise can be used to wrap the `setTimeout` to make the code more readable
139
118
- Function can take delay as argument and return a promise which gets resolved after timeout is complete
@@ -160,8 +139,7 @@ timeoutPromise.then(() => {
160
139
161
140
<br />
162
141
163
-
#### Q5
164
-
### Convert the xmlHTTPRequest to promise based function to get the data from the server asynchronously (fetch)
142
+
### Q. Convert the xmlHTTPRequest to promise based function to get the data from the server asynchronously (fetch)
165
143
166
144
- The Promise can be used to wrap the XHR request and provide cleaner interface to user for AJAX requests
167
145
- Success and failure of the XHR request can be handled to resolve or reject the promise respectively
@@ -208,8 +186,7 @@ XHR reuqest is no more in use and all the modern browsers use `fetch` API which
208
186
209
187
<br />
210
188
211
-
#### Q6
212
-
### Make a fetch request to retrieve and store JSON data from server
189
+
### Q. Make a fetch request to retrieve and store JSON data from server
213
190
214
191
- Fetch API is provided by the browser which returns a promise
215
192
- Fetch takes url as the 1st argument and an object with request details as 2nd optional argument
@@ -241,8 +218,7 @@ response.then(response => {
241
218
242
219
<br />
243
220
244
-
#### Q7
245
-
### Cancel a fetch request
221
+
### Q. Cancel a fetch request
246
222
247
223
-`AbortController` is an interface which can be used to abort a fetch request
248
224
-`signal` object of the AbortController object can be used as the part of the argument to `fetch` and abort on controller object can be used to stop the request
@@ -266,8 +242,7 @@ controller.abort();
266
242
267
243
<br />
268
244
269
-
#### Q8
270
-
### Show the working of async await work with promises
245
+
### Q. Show the working of async await work with promises
271
246
272
247
-`async` functions are asynchronous functions in which the asynchrous code can be executed in synchronous looking manner using `await`
273
248
-`await` expects a promise and the execution will stop until the promise is resolved
@@ -294,8 +269,7 @@ async function asyncAwaitFunc() {
294
269
295
270
<br />
296
271
297
-
#### Q9
298
-
### Write a code to resolve all the list of asynchronous executions of promises and stop if any of them is rejected. Print the output accordingly
272
+
### Q. Write a code to resolve all the list of asynchronous executions of promises and stop if any of them is rejected. Print the output accordingly
299
273
300
274
-`Promise.all` is the method which helps to achieve the functionality which settles if all the promises are resolved or any of them are rejected
301
275
- It receives array of promises as an argument to it
@@ -340,8 +314,7 @@ On failure of one of the promise, rest of the pending promises will be cancelled
340
314
341
315
<br />
342
316
343
-
#### Q10
344
-
### Write a code to resolve all the list of asynchronous executions of promises no matter if each execution succeeds or fails. Print the output of each
317
+
### Q. Write a code to resolve all the list of asynchronous executions of promises no matter if each execution succeeds or fails. Print the output of each
345
318
346
319
-`Promise.allSettled` is the method which helps to achieve the functionality which completes after all promises settle no matter of failures
347
320
- It receives array of promises as an argument to it
### Explain the working of Promise.race with few asynchronous function example
361
+
### Q. Explain the working of Promise.race with few asynchronous function example
390
362
391
363
- The `Promise.race` method returns a promise that fulfills or rejects as soon as one of the promises fulfills or rejects, with the success or failure
392
364
@@ -426,8 +398,7 @@ Promise.race(promiseArr).then(console.log).catch(console.log); // Rejected as
426
398
427
399
<br />
428
400
429
-
#### Q12
430
-
### Show me the working of a generator function
401
+
### Q. Show me the working of a generator function
431
402
432
403
- Generators are functions that can be exited and later re-entered
433
404
- Generator function will have `*` after the keyword `function`
@@ -457,8 +428,7 @@ Data between generator and iterator can be passed in both direction
457
428
458
429
<br />
459
430
460
-
#### Q13
461
-
### Write a generator function which uses another generator function internally to fetch the values. Use for..of loop to consume the values
431
+
### Q. Write a generator function which uses another generator function internally to fetch the values. Use for..of loop to consume the values
462
432
463
433
- Generator with the generator can be used to fetch the values using `yield*`
464
434
- The code consuming the parent generator need to be aware of it and can be used directly
@@ -487,8 +457,7 @@ for (let value of gen1()) {
487
457
488
458
<br />
489
459
490
-
#### Q14
491
-
### Write an interface to mock Promise in JavaScript which can be called to create a promise with resolve and reject. Also implement then functionality
460
+
### Q. Write an interface to mock Promise in JavaScript which can be called to create a promise with resolve and reject. Also implement then functionality
492
461
493
462
- Basic promise interface will allow creation of promise by passing resolver as the argument
494
463
-`resolve` and `reject` methods will have to be passed to the resolver which the user code will ivoke with data once settled
@@ -557,8 +526,7 @@ ES6 Promise is much more complex and sophesticated than the above shown implemen
557
526
558
527
<br />
559
528
560
-
#### Q15
561
-
### Write a function which helps to achieve the `Promise.all` functionality using promises
529
+
### Q. Write a function which helps to achieve the `Promise.all` functionality using promises
562
530
563
531
-`Promise.all` method is fail fast procedure to return all the promise resolved data in array or failed reason
564
532
@@ -606,8 +574,7 @@ PromiseAll([
606
574
607
575
<br />
608
576
609
-
#### Q16
610
-
### Show the working generator function with promises
577
+
### Q. Show the working generator function with promises
611
578
612
579
- Generator can be used with promises where yield will return a promise and promise resolution can trigger continuation
613
580
- Helper function is used to manage this flow which takes generator function as an argument and executes it
0 commit comments