Skip to content

Commit 6dc7bb1

Browse files
committed
Improve code style
1 parent c5f3606 commit 6dc7bb1

File tree

7 files changed

+36
-45
lines changed

7 files changed

+36
-45
lines changed

JavaScript/1-callbacks.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
const fs = require('node:fs');
44

55
fs.readFile('file1.txt', 'utf8', (err, data) => {
6-
console.log(err || data.toString());
6+
if (err) console.error(err);
7+
else console.log({ data });
78
fs.readFile('file2.txt', 'utf8', (err, data) => {
8-
console.log(err || data.toString());
9+
if (err) console.error(err);
10+
else console.log({ data });
911
fs.readFile('file3.txt', 'utf8', (err, data) => {
10-
console.log(err || data.toString());
12+
if (err) console.error(err);
13+
else console.log({ data });
1114
});
1215
});
1316
});

JavaScript/4-catch.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@
22

33
const fs = require('node:fs');
44

5-
const readTextFile = (filename) => fs.promises.readFile(filename, 'utf8');
5+
const readTextFile = (filename) => (
6+
fs.promises.readFile(filename, 'utf8')
7+
);
68

7-
readTextFile('file1-.txt')
8-
.then(
9-
(data) => {
10-
console.dir({ file1: data });
11-
return readTextFile('file2.txt');
12-
},
13-
(reason) => {
14-
console.log('Cannot read file1.txt --- A');
15-
console.log(reason);
16-
}
17-
)
18-
.catch(
19-
(reason) => {
20-
console.log('Cannot read file1.txt --- B');
21-
console.log(reason);
22-
}
23-
)
9+
readTextFile('file1.txt')
10+
.then((data) => {
11+
console.dir({ file1: data });
12+
return readTextFile('file2.txt');
13+
}, (reason) => {
14+
console.log('Cannot read file1.txt --- A');
15+
console.log(reason);
16+
})
17+
.catch((reason) => {
18+
console.log('Cannot read file1.txt --- B');
19+
console.log(reason);
20+
})
2421
.then((data) => {
2522
console.dir({ file2: data });
2623
return readTextFile('file3.txt');

JavaScript/5-finally.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
const fs = require('node:fs');
44

5-
const readTextFile = (filename) => fs.promises.readFile(filename, 'utf8');
6-
7-
readTextFile('file1-.txt')
5+
fs.promises.readFile('file1.txt', 'utf8')
86
.then((data) => {
97
console.dir({ file1: data });
108
})

JavaScript/8-all.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const baseUrl = 'http://localhost:3000';
77
const promises = [
88
fetch(baseUrl + '/person'),
99
fetch(baseUrl + '/'),
10-
fetch(baseUrl + '/city')
10+
fetch(baseUrl + '/city'),
1111
];
1212

1313
Promise.all(promises)

JavaScript/9-race.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const baseUrl = 'http://localhost:3000';
77
const promises = [
88
fetch(baseUrl + '/person'),
99
fetch(baseUrl + '/'),
10-
fetch(baseUrl + '/city')
10+
fetch(baseUrl + '/city'),
1111
];
1212

1313
Promise.race(promises)

JavaScript/a-thenable.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@
33
const fs = require('node:fs');
44

55
class Thenable {
6-
constructor() {
7-
this.next = null;
8-
}
6+
next = null;
97

108
then(fn) {
119
this.fn = fn;
12-
const next = new Thenable();
13-
this.next = next;
14-
return next;
10+
this.next = new Thenable();
11+
return this.next;
1512
}
1613

1714
resolve(value) {
18-
const fn = this.fn;
19-
if (fn) {
20-
const next = fn(value);
21-
if (next) {
22-
next.then((value) => {
23-
this.next.resolve(value);
24-
});
25-
}
26-
}
15+
if (!this.fn) return;
16+
const next = this.fn(value);
17+
if (!next) return;
18+
next.then((value) => {
19+
this.next.resolve(value);
20+
});
2721
}
2822
}
2923

JavaScript/server.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ const routes = {
66
'/': (request, callback) => {
77
callback({
88
apiVersion: '1.0',
9-
resources: ['person', 'city']
9+
resources: ['person', 'city'],
1010
});
1111
},
1212

1313
'/person': (request, callback) => {
1414
callback({
1515
name: 'Alex',
16-
age: 19
16+
age: 19,
1717
});
1818
},
1919

2020
'/city': (request, callback) => {
2121
callback({
2222
name: 'Kyiv',
23-
country: 'Ukraine'
23+
country: 'Ukraine',
2424
});
2525
}
2626
};
@@ -32,7 +32,6 @@ const server = http.createServer((req, res) => {
3232
res.end('Not found');
3333
return;
3434
}
35-
3635
handler(req, (result) => {
3736
const json = JSON.stringify(result);
3837
res.writeHead(200, { 'Content-Type': 'application/json' });

0 commit comments

Comments
 (0)