Skip to content

Commit 2b01b5d

Browse files
committed
Add more simple examples
1 parent d545c78 commit 2b01b5d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

JavaScript/7-async-catch.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const sum = async (a, b) => {
4+
if (typeof a === 'number' && typeof b === 'number') {
5+
return a + b;
6+
}
7+
throw new Error('a and b should be numbers');
8+
};
9+
10+
(async () => {
11+
12+
const x = 2;
13+
const y = 3;
14+
const total = await sum(x, y).catch((err) => {
15+
console.error({ x, y, err });
16+
return NaN;
17+
});
18+
console.log({ x, y, total });
19+
20+
const z = 7;
21+
const c = 'A';
22+
const res = await sum(z, c).catch((err) => {
23+
console.error({ z, c, err });
24+
return NaN;
25+
});
26+
console.log({ z, c, res });
27+
28+
})();

JavaScript/8-promise-all.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const sum = async (a, b) => {
4+
if (typeof a === 'number' && typeof b === 'number') {
5+
return a + b;
6+
}
7+
throw new Error('a and b should be numbers');
8+
};
9+
10+
(async () => {
11+
12+
const data = [
13+
[2, 3],
14+
[7, 'A'],
15+
];
16+
17+
const [total, result] = await Promise.all(
18+
data.map((args) =>
19+
sum(...args).catch((err) => {
20+
console.error(err);
21+
})
22+
)
23+
);
24+
console.log({ total, result });
25+
26+
})();

0 commit comments

Comments
 (0)