Skip to content

Commit 2adb577

Browse files
authored
Update async-challenges.md
1 parent dd71fbb commit 2adb577

File tree

1 file changed

+22
-59
lines changed

1 file changed

+22
-59
lines changed

challenges/async-challenges.md

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,11 @@
22
<a href="/README.md#this-is-a-collection-of-modern-interview-code-challenges-on-javascript-suitable-for" id="home">Home</a>
33
</div>
44

5-
## 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+
<h2 align="center">JavaScript challenges on Asynchronous programming - challenges</h2>
6+
7+
<br>
8+
9+
### Q. Print "Hello, world" with a delay of 3 seconds
3010

3111
- setTimeout takes a function as the 1st argument and optional timeout delay & list of values as the function parameters
3212
- 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
5333

5434
<br />
5535

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
5837

5938
```js
6039
function callbackExec(callback) {
@@ -75,8 +54,7 @@ callbackExec(displayHello);
7554

7655
<br />
7756

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
8058

8159
```js
8260
const num1 = 1, num2 = 10;
@@ -102,8 +80,7 @@ In the 2nd solution, recursive setTimeout is used.
10280

10381
<br />
10482

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
10784

10885
- `setInterval` function repeats a block of code at every given timing event
10986
- `clearInterval` is used to stop the setInterval execution
@@ -120,8 +97,7 @@ const intervalId = setInterval(() => {
12097

12198
<br />
12299

123-
#### Q5
124-
### 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
125101

126102
- We can use 3rd parameter of setTimeout to pass the value of iteration which creates a new scope each time loop iterates
127103
- 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--) {
147123

148124
<br />
149125

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
152127

153128
- The functionality to start and stop can be exposed from a function which internally takes care of incrementing and displaying data
154129
- `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
191166

192167
<br />
193168

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
196170

197171
- The asynchronous function can be simulated using setTimeout which executes the callback
198172
- The array of functions execution can be managed by having a function which takes care of execution of all the async functions
@@ -242,8 +216,7 @@ callbackManager([asyncFunc1, asyncFunc2, asyncFunc3]);
242216

243217
<br />
244218

245-
#### Q8
246-
### Execute the given list of asynchronous functions in parallel and return the results as an array to the callback
219+
### Q. Execute the given list of asynchronous functions in parallel and return the results as an array to the callback
247220
```js
248221
// Example
249222
function asyncFunc1(callback) {
@@ -294,8 +267,7 @@ function asyncParallel(asyncFuncArr, callback) {
294267

295268
<br />
296269

297-
#### Q9
298-
### Execute 3 asynchronous functions one after the other in sequence using promise chaining
270+
### Q. Execute 3 asynchronous functions one after the other in sequence using promise chaining
299271

300272
- The implementation of chaining is that the result is passed through the chain of `then` handlers for all the promises
301273
- `then` method on Promise also returns a promise which can be used to perform `then` on the returned promise
@@ -344,8 +316,7 @@ If `then` method has a return statement which is a promise then it will be consi
344316

345317
<br />
346318

347-
#### Q10
348-
### Execute 3 asynchronous functions one after the other in sequence using async await
319+
### Q. Execute 3 asynchronous functions one after the other in sequence using async await
349320

350321
- Async function with `await` for each promise can be used to execute in sequence
351322

@@ -365,8 +336,7 @@ If `then` method has a return statement which is a promise then it will be consi
365336
366337
<br />
367338
368-
#### Q11
369-
### Execute 3 asynchronous functions one after the other in sequence using promise chaining and do not terminate on failure
339+
### Q. Execute 3 asynchronous functions one after the other in sequence using promise chaining and do not terminate on failure
370340
371341
- The promise which gets rejected will invoke the 2nd function argument to `then` handler
372342
- The failure handler will receive the error and continue with next execution which will not propagate failures
@@ -403,8 +373,7 @@ async1()
403373
404374
<br />
405375
406-
#### Q12
407-
### Execute 3 asynchronous functions one after the other in sequence using async await and do not terminate on failure
376+
### Q. Execute 3 asynchronous functions one after the other in sequence using async await and do not terminate on failure
408377
409378
- Unlike promises, `try-catch` block can be used on async functions
410379
- `catch` block for each asynchronous function can be used to catch errors and continue with next execution which will not propagate failures
@@ -435,8 +404,7 @@ async1()
435404
436405
<br />
437406
438-
#### Q13
439-
### Execute an array of asynchronous functions which returns a promise, one after the other in sequence
407+
### Q. Execute an array of asynchronous functions which returns a promise, one after the other in sequence
440408
441409
- Asynchronous functions can be executed and promises can be captured in an array
442410
- Array method `reduce` can be used to make the sequential execution on promise settlement
@@ -463,8 +431,7 @@ asyncFuncArr.reduce(async (acc, asyncFunc) => {
463431
464432
<br />
465433
466-
#### Q14
467-
### 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
468435
469436
- Array method `reduce` can be used to make the simultaneously execution on promise settlement
470437
- Unlike sequential execution, the parallel execution of asynchronous functions happen but the output will executed in order of sequence
@@ -491,8 +458,7 @@ asyncFuncArr
491458
492459
<br />
493460
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
496462
497463
- `Promise.race` is an in built JavaScript method which helps us to return the first resolved or rejected promise data from promises array
498464
- Timeout feature can be set by adding a function returning a promise which rejects after specified amount of time
@@ -513,8 +479,7 @@ Promise.race(promiseArr).then(console.log).catch(console.log);
513479
514480
<br />
515481
516-
#### Q16
517-
### 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
518483
519484
- Utility can designed which returns a promise which attempts to make requests and return the data on success
520485
- The `fetch` request attempts to make calls after increasing time delay on failure
@@ -554,8 +519,7 @@ requestManager('https://reqbin.com/echo/get/json', 3).then(
554519
555520
<br />
556521
557-
#### Q17
558-
### Create a generator to return a random number on every request
522+
### Q. Create a generator to return a random number on every request
559523
560524
- The generation of random number can be implemented in the normal way in the function but will returned and function yields
561525
- The function will again continue to execute in loop to return a new random number
@@ -581,8 +545,7 @@ Genertor function need not complete its execution
581545
582546
<br />
583547
584-
#### Q18
585-
### Search for the presence of a given value in the nested object using generator
548+
### Q. Search for the presence of a given value in the nested object using generator
586549
587550
- With the help of generator Inversion of control is possible
588551
- Instead of function seaching for the key by passing the callback or key, the logic can be implemented in the controlling code

0 commit comments

Comments
 (0)