Skip to content

Commit 94af200

Browse files
committed
Improve code style
1 parent e2d71f2 commit 94af200

File tree

6 files changed

+14
-5
lines changed

6 files changed

+14
-5
lines changed

JavaScript/1-callback.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ sum(2, 3, (res) => {
1818
const total = (a, b, callback) => callback(null, a + b);
1919

2020
total(2, 3, (error, res) => {
21+
if (error) console.error(error);
2122
console.log(`sum(2, 3) = ${res}`);
2223
});

JavaScript/2-callback-use.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const timer = setInterval(() => {
3232
const fs = require('node:fs');
3333

3434
fs.readFile('./1-callback.js', 'utf8', (err, data) => {
35+
if (err) console.error(err);
3536
console.log({ lines: data.split('\n').length });
3637
});
3738

JavaScript/3-callback-named.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const fs = require('node:fs');
1717

1818
{
1919
const print = (file) => (err, data) => {
20+
if (err) console.error(err);
2021
console.log({ file, lines: data.split('\n').length });
2122
};
2223

JavaScript/Tasks/5-errors.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ const calculateSubtotal = (goods, callback) => {
3030
const calculateTotal = (order, callback) => {
3131
const expenses = new Map();
3232
let total = 0;
33+
const calc = (groupName) => (amount) => {
34+
total += amount;
35+
expenses.set(groupName, amount);
36+
};
3337
for (const groupName in order) {
3438
const goods = order[groupName];
35-
calculateSubtotal(goods, (amount) => {
36-
total += amount;
37-
expenses.set(groupName, amount);
38-
});
39+
calculateSubtotal(goods, calc(groupName));
3940
if (total > MAX_PURCHASE) {
4041
throw new Error('Total is above the limit');
4142
}

JavaScript/b-errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ try {
3737
a1(75);
3838
a1(100);
3939
a1(-200)(50)(30);
40-
} catch (e) {
40+
} catch {
4141
console.log('Never');
4242
}
4343

JavaScript/c-hell.js

+5
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ const writeReport = (path, data, callback) => {
3030
// Callback hell example
3131

3232
readConfig('config.json', (error, config) => {
33+
if (error) console.error(error);
3334
const min = config.population.minimalLimit;
3435
const sql = `select * from cities where size > ${min}`;
3536
selectFromDb(sql, (error, cities) => {
37+
if (error) console.error(error);
3638
const url = 'http://domain.name/api';
3739
apiRequest(url, cities, (error, stats) => {
40+
if (error) console.error(error);
3841
generateReport(cities, stats, (error, data) => {
42+
if (error) console.error(error);
3943
writeReport('README.md', data, (error, res) => {
44+
if (error) console.error(error);
4045
if (res.success) console.log('All done!');
4146
});
4247
});

0 commit comments

Comments
 (0)