Skip to content

Commit a01fee7

Browse files
committed
Promises
1 parent 4b4dc80 commit a01fee7

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var logUpdate = require('log-update');
2+
var toX = () => 'X';
3+
4+
var delay = (seconds) => new Promise((resolves) => {
5+
setTimeout(resolves, seconds*1000);
6+
});
7+
8+
var tasks = [
9+
delay(4),
10+
delay(6),
11+
delay(4),
12+
delay(3),
13+
delay(5),
14+
delay(7),
15+
delay(9),
16+
delay(10),
17+
delay(3),
18+
delay(5)
19+
];
20+
21+
class PromiseQueue {
22+
23+
constructor(promises=[], concurrentCount=1) {
24+
this.concurrent = concurrentCount;
25+
this.total = promises.length;
26+
this.todo = promises;
27+
this.running = [];
28+
this.complete = [];
29+
}
30+
31+
get runAnother() {
32+
return (this.running.length < this.concurrent) && this.todo.length;
33+
}
34+
35+
graphTasks() {
36+
var { todo, running, complete } = this;
37+
logUpdate(`
38+
39+
todo: [${todo.map(toX)}]
40+
running: [${running.map(toX)}]
41+
complete: [${complete.map(toX)}]
42+
43+
`);
44+
}
45+
46+
run() {
47+
while (this.runAnother) {
48+
var promise = this.todo.shift();
49+
promise.then(() => {
50+
this.complete.push(this.running.shift());
51+
this.graphTasks();
52+
this.run();
53+
})
54+
this.running.push(promise);
55+
this.graphTasks();
56+
}
57+
}
58+
59+
}
60+
61+
var delayQueue = new PromiseQueue(tasks, 2);
62+
delayQueue.run();

Advance Nodejs/7.1. Async.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var fs = require('fs');
2+
var { promisify } = require('util');
3+
var writeFile = promisify(fs.writeFile);
4+
var unlink = promisify(fs.unlink);
5+
var readdir = promisify(fs.readdir);
6+
var beep = () => process.stdout.write("\x07");
7+
var delay = (seconds) => new Promise((resolves) => {
8+
setTimeout(resolves, seconds*1000);
9+
})
10+
11+
async function start() {
12+
13+
var files = await readdir(__dirname);
14+
console.log(files);
15+
16+
}
17+
18+
start();

Advance Nodejs/8. Promise.all.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var fs = require('fs');
2+
var { promisify } = require('util');
3+
var writeFile = promisify(fs.writeFile);
4+
var unlink = promisify(fs.unlink);
5+
var readdir = promisify(fs.readdir);
6+
var beep = () => process.stdout.write("\x07");
7+
var delay = (seconds) => new Promise((resolves) => {
8+
setTimeout(resolves, seconds*1000);
9+
})
10+
11+
Promise.all([
12+
delay(5),
13+
delay(2),
14+
delay(3),
15+
delay(5)
16+
]).then(() => readdir(__dirname))
17+
.then(console.log);
18+
19+
20+
Promise.race([
21+
delay(5),
22+
delay(2),
23+
delay(3),
24+
delay(5)
25+
]).then(() => readdir(__dirname))
26+
.then(console.log);

Advance Nodejs/9. PromiseQueue.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var logUpdate = require('log-update');
2+
var toX = () => 'X';
3+
4+
var delay = (seconds) => new Promise((resolves) => {
5+
setTimeout(resolves, seconds*1000);
6+
});
7+
8+
var tasks = [
9+
delay(4),
10+
delay(6),
11+
delay(4),
12+
delay(3),
13+
delay(5),
14+
delay(7),
15+
delay(9),
16+
delay(10),
17+
delay(3),
18+
delay(5)
19+
];
20+
21+
class PromiseQueue {
22+
23+
constructor(promises=[], concurrentCount=1) {
24+
this.concurrent = concurrentCount;
25+
this.total = promises.length;
26+
this.todo = promises;
27+
this.running = [];
28+
this.complete = [];
29+
}
30+
31+
get runAnother() {
32+
return (this.running.length < this.concurrent) && this.todo.length;
33+
}
34+
35+
graphTasks() {
36+
var { todo, running, complete } = this;
37+
logUpdate(`
38+
39+
todo: [${todo.map(toX)}]
40+
running: [${running.map(toX)}]
41+
complete: [${complete.map(toX)}]
42+
43+
`);
44+
}
45+
46+
run() {
47+
while (this.runAnother) {
48+
var promise = this.todo.shift();
49+
promise.then(() => {
50+
this.complete.push(this.running.shift());
51+
this.graphTasks();
52+
this.run();
53+
})
54+
this.running.push(promise);
55+
this.graphTasks();
56+
}
57+
}
58+
59+
}
60+
61+
var delayQueue = new PromiseQueue(tasks, 2);
62+
delayQueue.run();

0 commit comments

Comments
 (0)