Skip to content

Commit d545c78

Browse files
committed
Improve examples
1 parent 7c98142 commit d545c78

File tree

6 files changed

+78
-37
lines changed

6 files changed

+78
-37
lines changed

JavaScript/1-sync-try.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ const sum = (a, b) => {
77
throw new Error('a and b should be numbers');
88
};
99

10+
const x = 2;
11+
const y = 3;
1012
try {
11-
console.log(sum(2, 3));
13+
const total = sum(x, y);
14+
console.log({ x, y, total });
1215
} catch (err) {
13-
console.log(err.message);
16+
console.error({ x, y, err });
1417
}
1518

19+
const z = 7;
20+
const c = 'A';
1621
try {
17-
console.log(sum(7, 'A'));
22+
const res = sum(z, c);
23+
console.log({ z, c, res });
1824
} catch (err) {
19-
console.log(err.message);
25+
console.error({ z, c, err });
2026
}
21-
22-
console.log(sum(7, 'A'));
23-
24-
console.log('Not executed');

JavaScript/2-sync-tuple.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ const sum = (a, b) => {
77
return [new Error('a and b should be numbers')];
88
};
99

10-
console.log(sum(2, 3));
10+
{
11+
const x = 2;
12+
const y = 3;
13+
const [err, total] = sum(x, y);
14+
if (err) console.error({ x, y, err, total });
15+
else console.log({ x, y, err, total });
16+
}
1117

12-
console.log(sum(7, 'A'));
18+
{
19+
const z = 7;
20+
const c = 'A';
21+
const [err, res = NaN] = sum(z, c);
22+
if (err) console.error({ z, c, err, res });
23+
else console.log({ z, c, err, res });
24+
}

JavaScript/3-async.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ const sum = (a, b, callback) => {
88
}
99
};
1010

11-
sum(2, 3, (err, result) => {
11+
const x = 2;
12+
const y = 3;
13+
sum(x, y, (err, total) => {
1214
if (err) {
13-
console.log(err.message);
15+
console.error({ x, y, err });
1416
return;
1517
}
16-
console.log(result);
18+
console.log({ x, y, total });
1719
});
1820

19-
sum(7, 'A', (err, result) => {
21+
const z = 7;
22+
const c = 'A';
23+
sum(z, c, (err, res) => {
2024
if (err) {
21-
console.log(err.message);
25+
console.error({ z, c, err });
2226
return;
2327
}
24-
console.log(result);
28+
console.log({ z, c, res });
2529
});

JavaScript/4-uncaught.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,16 @@ const sum = (a, b) => {
1212
throw new Error('a and b should be numbers');
1313
};
1414

15-
console.log(sum(2, 3));
16-
console.log(sum(7, 'A'));
15+
{
16+
const x = 2;
17+
const y = 3;
18+
const total = sum(x, y);
19+
console.log({ x, y, total });
20+
}
21+
22+
{
23+
const z = 7;
24+
const c = 'A';
25+
const res = sum(z, c);
26+
console.log({ z, c, res });
27+
}

JavaScript/5-promise.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
'use strict';
22

3-
const sum = (a, b) => new Promise((resolve, reject) => {
3+
const sum = (a, b) => new Promise((totalolve, reject) => {
44
if (typeof a === 'number' && typeof b === 'number') {
5-
resolve(a + b);
5+
totalolve(a + b);
66
} else {
77
reject(new Error('a and b should be numbers'));
88
}
99
});
1010

11-
sum(2, 3)
12-
.then((data) => {
13-
console.log(data);
11+
const x = 2;
12+
const y = 3;
13+
sum(x, y)
14+
.then((total) => {
15+
console.log({ x, y, total });
1416
})
1517
.catch((err) => {
16-
console.log(err.message);
18+
console.error({ x, y, err });
1719
});
1820

19-
sum(7, 'A')
20-
.then((data) => {
21-
console.log(data);
21+
const z = 7;
22+
const c = 'A';
23+
sum(z, c)
24+
.then((res) => {
25+
console.log({ z, c, res });
2226
})
2327
.catch((err) => {
24-
console.log(err.message);
28+
console.log({ z, c, err });
2529
});
2630

2731
/*
28-
sum(7, 'A')
29-
.then(data => {
30-
console.log(data);
32+
sum(z, c)
33+
.then((res) => {
34+
console.log({ z, c, res });
3135
});
3236
3337
UnhandledPromiseRejectionWarning: Error: a and b should be numbers

JavaScript/6-async-await.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,33 @@ const sum = async (a, b) => {
99

1010
(async () => {
1111

12+
const x = 2;
13+
const y = 3;
1214
try {
13-
console.log(await sum(2, 3));
14-
} catch (e) {
15-
console.log(e.message);
15+
const total = await sum(x, y);
16+
console.log({ x, y, total });
17+
} catch (err) {
18+
console.error({ x, y, err });
1619
}
1720

21+
const z = 7;
22+
const c = 'A';
1823
try {
19-
console.log(await sum(7, 'A'));
24+
const res = await sum(z, c);
25+
console.log({ z, c, res });
2026
} catch (err) {
21-
console.log(err.message);
27+
console.error({ z, c, err });
2228
}
2329

2430
/*
25-
console.log(await sum(7, 'A'));
31+
const res = await sum(z, c);
32+
console.log({ z, c, res });
2633
2734
UnhandledPromiseRejectionWarning: Error: a and b should be numbers
2835
2936
DeprecationWarning: Unhandled promise rejections are deprecated.
3037
In the future, promise rejections that are not handled will terminate
3138
the Node.js process with a non-zero exit code.
3239
*/
40+
3341
})();

0 commit comments

Comments
 (0)