Skip to content

Commit 43b2574

Browse files
committed
Add recursion examples
1 parent 9925fc8 commit 43b2574

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

‎JavaScript/5-parellel.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
class Proactor {
4+
#tasks = [];
5+
6+
enqueue(fn, callback) {
7+
this.#tasks.push({ fn, callback });
8+
}
9+
10+
start() {
11+
while (this.#tasks.length) {
12+
const tasks = this.#tasks.splice(0);
13+
this.#next(tasks, 0);
14+
}
15+
}
16+
17+
#next(tasks, offset) {
18+
if (offset >= tasks.length) return;
19+
const handler = tasks[offset];
20+
handler.fn((err, data) => {
21+
handler.callback(err, data);
22+
});
23+
this.#next(tasks, offset + 1);
24+
}
25+
}
26+
27+
// Usage
28+
29+
const eventLoop = new Proactor();
30+
31+
const fakeAsyncRead = (name, callback) => {
32+
const delay = Math.random() * 1000;
33+
const fn = (done) => {
34+
setTimeout(() => {
35+
done(null, 'FILE DATA');
36+
}, delay);
37+
};
38+
eventLoop.enqueue(fn, callback);
39+
};
40+
41+
fakeAsyncRead('File A', (err, data) => {
42+
if (err) console.error(`[Proactor] File A: failed "${err.message}"`);
43+
else console.log(`[Proactor] File A: done "${data}"`);
44+
});
45+
46+
fakeAsyncRead('File B', (err, data) => {
47+
if (err) console.error(`[Proactor] File B: failed "${err.message}"`);
48+
else console.log(`[Proactor] File B: done "${data}"`);
49+
});
50+
51+
fakeAsyncRead('File C', (err, data) => {
52+
if (err) console.error(`[Proactor] File C: failed "${err.message}"`);
53+
else console.log(`[Proactor] File C: done "${data}"`);
54+
});
55+
56+
eventLoop.start();

‎JavaScript/6-for.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
class Proactor {
4+
#tasks = [];
5+
6+
enqueue(fn, callback) {
7+
this.#tasks.push({ fn, callback });
8+
}
9+
10+
start() {
11+
if (!this.#tasks.length) return;
12+
const tasks = this.#tasks.splice(0);
13+
this.#phase(tasks);
14+
}
15+
16+
#phase(tasks) {
17+
let counter = tasks.legth;
18+
for (const task of tasks) {
19+
task.fn((err, data) => {
20+
task.callback(err, data);
21+
if (--counter === 0) this.start();
22+
});
23+
}
24+
}
25+
}
26+
27+
// Usage
28+
29+
const eventLoop = new Proactor();
30+
31+
const fakeAsyncRead = (name, callback) => {
32+
const delay = Math.random() * 1000;
33+
const fn = (done) => {
34+
setTimeout(() => {
35+
done(null, 'FILE DATA');
36+
}, delay);
37+
};
38+
eventLoop.enqueue(fn, callback);
39+
};
40+
41+
fakeAsyncRead('File A', (err, data) => {
42+
if (err) console.error(`[Proactor] File A: failed "${err.message}"`);
43+
else console.log(`[Proactor] File A: done "${data}"`);
44+
});
45+
46+
fakeAsyncRead('File B', (err, data) => {
47+
if (err) console.error(`[Proactor] File B: failed "${err.message}"`);
48+
else console.log(`[Proactor] File B: done "${data}"`);
49+
});
50+
51+
fakeAsyncRead('File C', (err, data) => {
52+
if (err) console.error(`[Proactor] File C: failed "${err.message}"`);
53+
else console.log(`[Proactor] File C: done "${data}"`);
54+
});
55+
56+
eventLoop.start();

0 commit comments

Comments
 (0)