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 - challenges
6
-
7
-
1.[Print "Hello, world" with a delay of 3 seconds](#Q1)
8
-
1.[Create a function which receives a function as argument and executes it after 2 seconds](#Q2)
9
-
1.[Print numbers from 1 to 10 with delay of 1 second between each value being printed](#Q3)
10
-
1.[Print numbers from 1 to 10 with delay of 1 second between each value being printed using setInterval](#Q4)
11
-
1.[Print numbers from 10 to 1 with delay of 1 second between each value being printed using setTimeout using pre ES6 features only](#Q5)
12
-
1.[Write a utility which prints numbers starting from an initial value and increment in steps which can be started and stopped by the user, any number of times](#Q6)
13
-
1.[Execute an array of asynchronous functions one after the other in sequence using callbacks](#Q7)
14
-
1.[Execute the given list of asynchronous functions in parallel and return the results as an array to the callback](#Q8)
15
-
1.[Execute 3 asynchronous functions one after the other in sequence using promise chaining](#Q9)
16
-
1.[Execute 3 asynchronous functions one after the other in sequence using async await](#Q10)
17
-
1.[Execute 3 asynchronous functions one after the other in sequence using promise chaining and do not terminate on failure](#Q11)
18
-
1.[Execute 3 asynchronous functions one after the other in sequence using async await and do not terminate on failure](#Q12)
19
-
1.[Execute an array of asynchronous functions which returns a promise, one after the other in sequence](#Q13)
20
-
1.[Execute an array of asynchronous functions simultaneously but print the output in the ordered sequence. Do not wait for printing the data if it already available after promise is settled](#Q14)
21
-
1.[Design a utility which takes array of asynchronous functions and returns the 1st successful or non successful result with max waiting time set by the user](#Q15)
22
-
1.[Design a utility which takes URL and a value for attempts which will attempt to make a fetch request. If on failure it tries again with increasing delay for number of times which user has requested](#Q16)
23
-
1.[Create a generator to return a random number on every request](#Q17)
24
-
1.[Search for the presence of a given value in the nested object using generator](#Q18)
25
-
26
-
---
27
-
28
-
#### Q1
29
-
### Print "Hello, world" with a delay of 3 seconds
5
+
<h2align="center">JavaScript challenges on Asynchronous programming - challenges</h2>
6
+
7
+
<br>
8
+
9
+
### Q. Print "Hello, world" with a delay of 3 seconds
30
10
31
11
- setTimeout takes a function as the 1st argument and optional timeout delay & list of values as the function parameters
32
12
- setTimeout returns an id (number) which can be used to stop the setTimeout using clearTimeout function
@@ -53,8 +33,7 @@ Zero or more values that represent any parameters you want to pass to the functi
53
33
54
34
<br />
55
35
56
-
#### Q2
57
-
### Create a function which receives a function as argument and executes it after 2 seconds
36
+
### Q. Create a function which receives a function as argument and executes it after 2 seconds
58
37
59
38
```js
60
39
functioncallbackExec(callback) {
@@ -75,8 +54,7 @@ callbackExec(displayHello);
75
54
76
55
<br />
77
56
78
-
#### Q3
79
-
### Print numbers from 1 to 10 with delay of 1 second between each value being printed
57
+
### Q. Print numbers from 1 to 10 with delay of 1 second between each value being printed
80
58
81
59
```js
82
60
constnum1=1, num2 =10;
@@ -102,8 +80,7 @@ In the 2nd solution, recursive setTimeout is used.
102
80
103
81
<br />
104
82
105
-
#### Q4
106
-
### Print numbers from 1 to 10 with delay of 1 second between each value being printed using setInterval
83
+
### Q. Print numbers from 1 to 10 with delay of 1 second between each value being printed using setInterval
107
84
108
85
-`setInterval` function repeats a block of code at every given timing event
109
86
-`clearInterval` is used to stop the setInterval execution
### Print numbers from 10 to 1 with delay of 1 second between each value being printed using setTimeout using pre ES6 features only
100
+
### Q. Print numbers from 10 to 1 with delay of 1 second between each value being printed using setTimeout using pre ES6 features only
125
101
126
102
- We can use 3rd parameter of setTimeout to pass the value of iteration which creates a new scope each time loop iterates
127
103
- We can also use an inner function scope (IIFE) within the for loop for each iteration
@@ -147,8 +123,7 @@ for (var i = num1; i >= num2; i--) {
147
123
148
124
<br />
149
125
150
-
#### Q6
151
-
### Write a utility which prints numbers starting from an initial value and increment in steps which can be started and stopped by the user, any number of times
126
+
### Q. Write a utility which prints numbers starting from an initial value and increment in steps which can be started and stopped by the user, any number of times
152
127
153
128
- The functionality to start and stop can be exposed from a function which internally takes care of incrementing and displaying data
154
129
-`setInterval` can be used to achieve the task and handle the start & stop of data display
@@ -191,8 +166,7 @@ The function can also be modified to have completion after which timer can not b
191
166
192
167
<br />
193
168
194
-
#### Q7
195
-
### Execute an array of asynchronous functions one after the other in sequence using callbacks
169
+
### Q. Execute an array of asynchronous functions one after the other in sequence using callbacks
196
170
197
171
- The asynchronous function can be simulated using setTimeout which executes the callback
198
172
- The array of functions execution can be managed by having a function which takes care of execution of all the async functions
### Execute an array of asynchronous functions simultaneously but print the output in the ordered sequence. Do not wait for printing the data if it already available after promise is settled
434
+
### Q. Execute an array of asynchronous functions simultaneously but print the output in the ordered sequence. Do not wait for printing the data if it already available after promise is settled
468
435
469
436
- Array method `reduce` can be used to make the simultaneously execution on promise settlement
470
437
- Unlike sequential execution, the parallel execution of asynchronous functions happen but the output will executed in order of sequence
@@ -491,8 +458,7 @@ asyncFuncArr
491
458
492
459
<br />
493
460
494
-
#### Q15
495
-
### Design a utility which takes array of asynchronous functions and returns the 1st successful or non successful result with max waiting time set by the user
461
+
### Q. Design a utility which takes array of asynchronous functions and returns the 1st successful or non successful result with max waiting time set by the user
496
462
497
463
- `Promise.race` is an in built JavaScript method which helps us to return the first resolved or rejected promise data from promises array
498
464
- Timeout feature can be set by adding a function returning a promise which rejects after specified amount of time
### Design a utility which takes URL and a value for attempts which will attempt to make a fetch request. If on failure it tries again with increasing delay for number of times which user has requested
482
+
### Q. Design a utility which takes URL and a value for attempts which will attempt to make a fetch request. If on failure it tries again with increasing delay for number of times which user has requested
518
483
519
484
- Utility can designed which returns a promise which attempts to make requests and return the data on success
520
485
- The `fetch` request attempts to make calls after increasing time delay on failure
0 commit comments