Skip to content

Commit dd71fbb

Browse files
authored
Update async-concepts.md
1 parent e552ff4 commit dd71fbb

File tree

1 file changed

+20
-53
lines changed

1 file changed

+20
-53
lines changed

challenges/async-concepts.md

+20-53
Original file line numberDiff line numberDiff line change
@@ -2,29 +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 - 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+
<h2 align="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
2810

2911
- The asynchronous block of code can be a function which executes asynchronously
3012
- 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
5840

5941
<br />
6042

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
6344

6445
- XMLHttpRequest (XHR) objects are used to interact with server to retrieve data from a URL without having to do a full page refresh
6546
- 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
8869

8970
<br />
9071

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
9373

9474
- The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value
9575
- 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
132112

133113
<br />
134114

135-
#### Q4
136-
### Wrap the setTimeout function to convert to a promise
115+
### Q. Wrap the setTimeout function to convert to a promise
137116

138117
- Promise can be used to wrap the `setTimeout` to make the code more readable
139118
- Function can take delay as argument and return a promise which gets resolved after timeout is complete
@@ -160,8 +139,7 @@ timeoutPromise.then(() => {
160139

161140
<br />
162141

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)
165143

166144
- The Promise can be used to wrap the XHR request and provide cleaner interface to user for AJAX requests
167145
- 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
208186

209187
<br />
210188

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
213190

214191
- Fetch API is provided by the browser which returns a promise
215192
- Fetch takes url as the 1st argument and an object with request details as 2nd optional argument
@@ -241,8 +218,7 @@ response.then(response => {
241218

242219
<br />
243220

244-
#### Q7
245-
### Cancel a fetch request
221+
### Q. Cancel a fetch request
246222

247223
- `AbortController` is an interface which can be used to abort a fetch request
248224
- `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();
266242

267243
<br />
268244

269-
#### Q8
270-
### Show the working of async await work with promises
245+
### Q. Show the working of async await work with promises
271246

272247
- `async` functions are asynchronous functions in which the asynchrous code can be executed in synchronous looking manner using `await`
273248
- `await` expects a promise and the execution will stop until the promise is resolved
@@ -294,8 +269,7 @@ async function asyncAwaitFunc() {
294269

295270
<br />
296271

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
299273

300274
- `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
301275
- 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
340314

341315
<br />
342316

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
345318

346319
- `Promise.allSettled` is the method which helps to achieve the functionality which completes after all promises settle no matter of failures
347320
- It receives array of promises as an argument to it
@@ -385,8 +358,7 @@ const promiseArr = asyncArr.map(async => async());
385358

386359
<br />
387360

388-
#### Q11
389-
### Explain the working of Promise.race with few asynchronous function example
361+
### Q. Explain the working of Promise.race with few asynchronous function example
390362

391363
- 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
392364

@@ -426,8 +398,7 @@ Promise.race(promiseArr).then(console.log).catch(console.log); // Rejected as
426398

427399
<br />
428400

429-
#### Q12
430-
### Show me the working of a generator function
401+
### Q. Show me the working of a generator function
431402

432403
- Generators are functions that can be exited and later re-entered
433404
- Generator function will have `*` after the keyword `function`
@@ -457,8 +428,7 @@ Data between generator and iterator can be passed in both direction
457428

458429
<br />
459430

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
462432

463433
- Generator with the generator can be used to fetch the values using `yield*`
464434
- 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()) {
487457

488458
<br />
489459

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
492461

493462
- Basic promise interface will allow creation of promise by passing resolver as the argument
494463
- `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
557526

558527
<br />
559528

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
562530

563531
- `Promise.all` method is fail fast procedure to return all the promise resolved data in array or failed reason
564532

@@ -606,8 +574,7 @@ PromiseAll([
606574

607575
<br />
608576

609-
#### Q16
610-
### Show the working generator function with promises
577+
### Q. Show the working generator function with promises
611578

612579
- Generator can be used with promises where yield will return a promise and promise resolution can trigger continuation
613580
- Helper function is used to manage this flow which takes generator function as an argument and executes it

0 commit comments

Comments
 (0)