-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-promise.js
49 lines (46 loc) · 1.13 KB
/
example-promise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// function getTempCallback(location, callback) {
// callback(undefined, 78);
// callback('City not found');
// }
//
// getTempCallback('Philadelphia', function (err, temp) {
// if (err) {
// console.log('error', err);
// } else {
// console.log('succes', temp);
// }
// });
//
// function getTempPromise(location) {
// return new Promise(function (resolve, reject) {
// setTimeout(function () {
// resolve(79);
// reject('City not found');
// }, 1000);
// });
// }
//
// getTempPromise('Philadelphia').then(function (temp) {
// console.log('promise succes', temp);
// }, function (err) {
// console.log('promise error', err);
// });
function addPromise(a, b) {
return new Promise(function (resolve, reject) {
if (typeof a === 'number' && typeof b === 'number'){
resolve(a+b);
} else {
reject('een van de twee invoervelden is geen getal');
}
})
}
addPromise(1, 2).then(function (number) {
console.log('promise succes', number)
}, function (err) {
console.log('error', err)
});
addPromise(1, 'test').then(function (number) {
console.log('promise succes', number)
}, function (err) {
console.log('error', err)
});