-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-promise.js
58 lines (48 loc) · 1.15 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
50
51
52
53
54
55
56
57
58
/*function getTempCallback(location, callback) {
callback(undefined, 78);
callback('City not found');
};
getTempCallback('Philadelphia', (err, temp) => {
if (err) {
console.log('error', err);
} else {
console.log('success', temp);
}
});
function getTempPromise(location) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('success');
resolve(79);
}, 2000)
setTimeout(() => {
console.log('error');
reject('City not found');
}, 1000)
});
}
getTempPromise('Philadelphia').then((temp) => {
console.log('promise success', temp);
}, (err) => {
console.log('promise error', err);
});*/
// Challenge Area
function addPromice(a, b) {
return new Promise((resolve, reject) => {
if (typeof a === 'number' && typeof b === 'number' ) {
resolve(a + b);
} else {
reject('One or both parameters is not a number');
}
})
}
addPromice(6, 2).then((result) => {
console.log('Sum is', result);
}, (error) => {
console.log('Error:', error);
});
addPromice('6', 2).then((result) => {
console.log('Sum is', result);
}, (error) => {
console.log('Error:', error);
});