Skip to content

Commit 989e050

Browse files
committed
Add Promise example
1 parent cf23af0 commit 989e050

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

JavaScript/5-promise.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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(2, 3)
12+
.then(data => {
13+
console.log(data);
14+
})
15+
.catch(err => {
16+
console.log(err.message);
17+
});
18+
19+
sum(7, 'A')
20+
.then(data => {
21+
console.log(data);
22+
})
23+
.catch(err => {
24+
console.log(err.message);
25+
});

0 commit comments

Comments
 (0)