Skip to content

Commit 9304e79

Browse files
author
Devendra Parmar
committed
Solved the two questions of the JavaScript
1. Cast the provided value as an async 2. Chain asynchronous function
1 parent 8027e00 commit 9304e79

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Write a JavaScript program to cast the provided value as an array if it's not one.
2+
3+
const castValue = (val) => {
4+
return Array.isArray(val) ? val : [val];
5+
};
6+
7+
console.log(castValue("devendra"));
8+
console.log(castValue([123]));

October/ChainAsynchronousFunctions.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Write a JavaScript program to chain asynchronous functions.
2+
3+
// Note: Loop through an array of functions containing asynchronous events, calling next when each asynchronous event has completed.
4+
5+
const chainAsync = (fns) => {
6+
let cur = 0;
7+
8+
const next = () => fns[cur++](next);
9+
10+
next();
11+
};
12+
13+
chainAsync([
14+
(next) => {
15+
console.log("0 Seconds!");
16+
setTimeout(next, 1000);
17+
},
18+
(next) => {
19+
console.log("1 Seconds!");
20+
setTimeout(next, 1000);
21+
},
22+
(next) => {
23+
console.log("2 Seconds!");
24+
},
25+
]);

0 commit comments

Comments
 (0)