Skip to content

Commit 976bd89

Browse files
committed
Add .then and .finally examples
1 parent 0bb0d68 commit 976bd89

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

JavaScript/2-then.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const sum = (a, b) => new Promise((resolve, reject) => {
4+
if (typeof a === 'number' && typeof b === 'number') {
5+
resolve(a + b);
6+
} else {
7+
reject(new Error('a and b should be numbers'));
8+
}
9+
});
10+
11+
sum(7, 'A')
12+
.then(
13+
data => {
14+
console.log({ data });
15+
},
16+
err => {
17+
console.log({ messageThen: err.message });
18+
}
19+
)
20+
.catch(err => {
21+
console.log({ messageCatch: err.message });
22+
});

JavaScript/3-finally.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const sum = (a, b) => new Promise((resolve, reject) => {
4+
if (typeof a === 'number' && typeof b === 'number') {
5+
resolve(a + b);
6+
} else {
7+
reject(new Error('a and b should be numbers'));
8+
}
9+
});
10+
11+
sum(7, 'A')
12+
.then(data => {
13+
console.log({ data });
14+
})
15+
.catch(err => {
16+
console.log({ messageCatch: err.message });
17+
})
18+
.finally(() => {
19+
console.log('finally');
20+
});

0 commit comments

Comments
 (0)