8000 Code style · HowProgrammingWorks/Generator@2f9e775 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f9e775

Browse files
committed
Code style
1 parent 4e27669 commit 2f9e775

File tree

4 files changed

+37
-35
lines changed

4 files changed

+37
-35
lines changed
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
function* sleep(interval) {
4-
let start = new Date();
4+
const start = new Date();
55
while (new Date() - start < interval) {
66
yield;
77
}
@@ -18,23 +18,25 @@ function* consume() {
1818
let count = 0;
1919
let sum = 0;
2020
while (true) {
21-
let data = yield;
21+
const data = yield;
2222
count++;
2323
sum += data;
24-
console.log(`Got data: ${data}\n` +
25-
`Count: ${count}\n` +
26-
`Sum: ${sum}\n` +
27-
`Average: ${sum / count}\n`);
24+
console.log(
25+
`Got data: ${data}\n` +
26+
`Count: ${count}\n` +
27+
`Sum: ${sum}\n` +
28+
`Average: ${sum / count}\n`
29+
);
2830
}
2931
}
3032

31-
let consumer = consume();
32-
let producer = produce(consumer);
33+
const consumer = consume();
34+
const producer = produce(consumer);
3335
consumer.next();
3436

35-
function step() {
37+
const step = () => {
3638
producer.next();
3739
setImmediate(step);
38-
}
40+
};
3941

4042
step();

JavaScript/7-do-promise.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ const doMonad = require('./helpers/do-notation');
77

88
const baseUrl = 'http://localhost:3000/';
99
doMonad(function* () {
10-
let api = yield getJSON(baseUrl);
11-
for (let resource of api.resources) {
12-
let data = yield getJSON(baseUrl + resource);
10+
const api = yield getJSON(baseUrl);
11+
let resource, data;
12+
for (resource of api.resources) {
13+
data = yield getJSON(baseUrl + resource);
1314
console.log(data);
1415
}
1516
});

JavaScript/8-catch-promise.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ const doMonad = require('./helpers/do-notation');
77

88
const baseUrl = 'http://localhost:3000/';
99
doMonad(function* () {
10-
let api = yield getJSON(baseUrl);
11-
for (let resource of api.resources) {
12-
let data = yield getJSON(baseUrl + resource);
10+
const api = yield getJSON(baseUrl);
11+
let resource, data;
12+
for (resource of api.resources) {
13+
data = yield getJSON(baseUrl + resource);
1314
console.log(data);
1415
}
1516
}).catch(e => {

JavaScript/9-maybe-catch.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,39 @@
22

33
const doMonad = require('./helpers/do-notation');
44

5+
const Nothing = {
6+
then() {
7+
return Nothing;
8+
},
9+
10+
catch(callback) {
11+
callback();
12+
return this;
13+
}
14+
};
15+
516
class Just {
617
constructor(value) {
718
this.value = value;
819
}
920

1021
then(fn) {
11-
let result = fn(this.value);
12-
if (result instanceof Just ||
13-
result === Nothing) {
22+
const result = fn(this.value);
23+
if (result instanceof Just || result === Nothing) {
1424
return result;
15-
} else {
16-
return new Just(result);
1725
}
26+
return new Just(result);
1827
}
1928

2029
catch() {
2130
return this;
2231
}
2332
}
2433

25-
const Nothing = {
26-
then() {
27-
return Nothing;
28-
},
29-
30-
catch(callback) {
31-
callback();
32-
return this;
33-
}
34-
};
35-
3634
doMonad(function* () {
37-
let a = yield new Just(5);
38-
let b = yield new Just(3);
39-
let c = yield new Just(4);
35+
const a = yield new Just(5);
36+
const b = yield new Just(3);
37+
const c = yield new Just(4);
4038
console.log((a + b) * c);
4139
}).catch(() => {
4240
console.log('At least one value is not present');

0 commit comments

Comments
 (0)
0