Skip to content

Commit 59375e5

Browse files
author
Sadanand Pai
committed
Web: copy icon for challenges
1 parent 72bba18 commit 59375e5

6 files changed

Lines changed: 177 additions & 177 deletions

File tree

web/src/pages/challenges/async.mdx

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- setTimeout takes a function as the 1st argument and optional timeout delay & list of values as the function parameters
44
- setTimeout returns an id (number) which can be used to stop the setTimeout using clearTimeout function
55

6-
```js
6+
```js copy
77
setTimeout(
88
function (text) {
99
console.log(text);
@@ -13,11 +13,11 @@ setTimeout(
1313
);
1414
```
1515

16-
```js
16+
```js copy
1717
setTimeout(() => console.log("Hello, World"), 3000);
1818
```
1919

20-
```js
20+
```js copy
2121
setTimeout(console.log, 3000, "Hello, World");
2222
```
2323

@@ -33,7 +33,7 @@ Zero or more values that represent any parameters you want to pass to the functi
3333

3434
### 2. Create a function which receives a function as argument and executes it after 2 seconds
3535

36-
```js
36+
```js copy
3737
function callbackExec(callback) {
3838
if (typeof callback === "function") {
3939
setTimeout(() => {
@@ -54,15 +54,15 @@ callbackExec(displayHello);
5454

5555
### 3. Print numbers from 1 to 10 with delay of 1 second between each value being printed
5656

57-
```js
57+
```js copy
5858
const num1 = 1,
5959
num2 = 10;
6060
for (let i = num1; i <= num2; i++) {
6161
setTimeout(() => console.log(i), i * 1000);
6262
}
6363
```
6464

65-
```js
65+
```js copy
6666
const num1 = 1,
6767
num2 = 10;
6868
+(function displayWithDelay(i) {
@@ -86,7 +86,7 @@ In the 2nd solution, recursive setTimeout is used.
8686
- `setInterval` function repeats a block of code at every given timing event
8787
- `clearInterval` is used to stop the setInterval execution
8888

89-
```js
89+
```js copy
9090
const num1 = 1,
9191
num2 = 10;
9292
let i = num1;
@@ -103,15 +103,15 @@ const intervalId = setInterval(() => {
103103
- We can use 3rd parameter of setTimeout to pass the value of iteration which creates a new scope each time loop iterates
104104
- We can also use an inner function scope (IIFE) within the for loop for each iteration
105105

106-
```js
106+
```js copy
107107
var num1 = 10,
108108
num2 = 1;
109109
for (var i = num1; i >= num2; i--) {
110110
setTimeout(console.log, (num1 - i) * 1000, i);
111111
}
112112
```
113113

114-
```js
114+
```js copy
115115
var num1 = 10,
116116
num2 = 1;
117117
for (var i = num1; i >= num2; i--) {
@@ -134,7 +134,7 @@ for (var i = num1; i >= num2; i--) {
134134
- The functionality to start and stop can be exposed from a function which internally takes care of incrementing and displaying data
135135
- `setInterval` can be used to achieve the task and handle the start & stop of data display
136136

137-
```js
137+
```js copy
138138
function timer(init = 0, step = 1) {
139139
var intervalId;
140140
var count = init;
@@ -179,7 +179,7 @@ The function can also be modified to have completion after which timer can not b
179179
- The array of functions execution can be managed by having a function which takes care of execution of all the async functions
180180
- Asynchronous functions need not be aware of the function to be executed and will take a callback as argument and execute it after completion
181181

182-
```js
182+
```js copy
183183
function asyncFunc1(callback) {
184184
console.log("Started asyncFunc1");
185185
setTimeout(() => {
@@ -226,7 +226,7 @@ callbackManager([asyncFunc1, asyncFunc2, asyncFunc3]);
226226

227227
### 8. Execute the given list of asynchronous functions in parallel and return the results as an array to the callback
228228

229-
```js
229+
```js copy
230230
// Example
231231
function asyncFunc1(callback) {
232232
setTimeout(() => {
@@ -254,7 +254,7 @@ asyncParallel([asyncFunc1, asyncFunc2, asyncFunc3], (result) => {
254254
- The async functions can be executed in parallel using the loop and can be tracked for completion with a counter
255255
- The callback function can be sent to the async functions and the results will be stored in the array which will be returned after the completion of all
256256

257-
```js
257+
```js copy
258258
function asyncParallel(asyncFuncArr, callback) {
259259
const resultArr = new Array(asyncFuncArr.length);
260260
let resultCounter = 0;
@@ -283,7 +283,7 @@ function asyncParallel(asyncFuncArr, callback) {
283283
- `then` method on Promise also returns a promise which can be used to perform `then` on the returned promise
284284
- The errors in promise / promise chaining can be handled with the error callback for each promise when it settles or with a generic catch block
285285

286-
```js
286+
```js copy
287287
asyncFunc1().then(
288288
() => {
289289
console.log("Completed async1");
@@ -311,7 +311,7 @@ asyncFunc1().then(
311311
);
312312
```
313313

314-
```js
314+
```js copy
315315
asyncFunc1()
316316
.then(asyncFunc2)
317317
.then(asyncFunc3)
@@ -334,7 +334,7 @@ If `then` method has a return statement which is a promise then it will be consi
334334

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

337-
```js
337+
```js copy
338338
+async function executor(){
339339
try{
340340
await asyncFunc1();
@@ -355,7 +355,7 @@ If `then` method has a return statement which is a promise then it will be consi
355355
- The promise which gets rejected will invoke the 2nd function argument to `then` handler
356356
- The failure handler will receive the error and continue with next execution which will not propagate failures
357357
358-
```js
358+
```js copy
359359
async1()
360360
.then(
361361
() => {
@@ -392,7 +392,7 @@ async1()
392392
- Unlike promises, `try-catch` block can be used on async functions
393393
- `catch` block for each asynchronous function can be used to catch errors and continue with next execution which will not propagate failures
394394
395-
```js
395+
```js copy
396396
+(async function executor() {
397397
try {
398398
await asyncFunc1();
@@ -423,15 +423,15 @@ async1()
423423
- Asynchronous functions can be executed and promises can be captured in an array
424424
- Array method `reduce` can be used to make the sequential execution on promise settlement
425425
426-
```js
426+
```js copy
427427
const asyncFuncArr = [asyncFunc1, asyncFunc2, asyncFunc3];
428428
429429
asyncFuncArr.reduce((acc, async) => {
430430
return acc.then(() => async().then(console.log));
431431
}, Promise.resolve());
432432
```
433433
434-
```js
434+
```js copy
435435
const asyncFuncArr = [asyncFunc1, asyncFunc2, asyncFunc3];
436436
437437
asyncFuncArr.reduce(async (acc, asyncFunc) => {
@@ -451,7 +451,7 @@ asyncFuncArr.reduce(async (acc, asyncFunc) => {
451451
- Array method `reduce` can be used to make the simultaneously execution on promise settlement
452452
- Unlike sequential execution, the parallel execution of asynchronous functions happen but the output will executed in order of sequence
453453
454-
```js
454+
```js copy
455455
const asyncFuncArr = [asyncFunc1, asyncFunc2, asyncFunc3];
456456
457457
asyncFuncArr
@@ -461,7 +461,7 @@ asyncFuncArr
461461
}, Promise.resolve());
462462
```
463463
464-
```js
464+
```js copy
465465
const asyncFuncArr = [asyncFunc1, asyncFunc2, asyncFunc3];
466466
467467
asyncFuncArr
@@ -479,7 +479,7 @@ asyncFuncArr
479479
- Timeout feature can be set by adding a function returning a promise which rejects after specified amount of time
480480
- If any promise resolves before timeout the promise which settles first will be the output else timeout will cause rejection
481481
482-
```js
482+
```js copy
483483
function timeoutFunc() {
484484
const delay = 500;
485485
return new Promise((resolve, reject) => {
@@ -500,7 +500,7 @@ Promise.race(promiseArr).then(console.log).catch(console.log);
500500
- The `fetch` request attempts to make calls after increasing time delay on failure
501501
- If all the attempts by to get response fails, promise gets rejected
502502
503-
```js
503+
```js copy
504504
function requestManager(url, attempts = 3) {
505505
return new Promise(async (resolve, reject) => {
506506
for (let i = 0; i < attempts; i++) {
@@ -541,7 +541,7 @@ requestManager("https://reqbin.com/echo/get/json", 3).then(
541541
- The generation of random number can be implemented in the normal way in the function but will returned and function yields
542542
- The function will again continue to execute in loop to return a new random number
543543
544-
```js
544+
```js copy
545545
function* generatorFunc(param) {
546546
while (true) {
547547
yield Math.ceil(Math.random() * 100);
@@ -570,7 +570,7 @@ Genertor function need not complete its execution
570570
- Instead of function seaching for the key by passing the callback or key, the logic can be implemented in the controlling code
571571
- For..of loop calling the recursive generator function on object can be used to achieve this
572572
573-
```js
573+
```js copy
574574
function* objectReader(obj) {
575575
for (let key in obj) {
576576
if (typeof obj[key] === "object") {

0 commit comments

Comments
 (0)