колбеки function old callback if callback null else callback new Error

 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
// колбеки
function old(a, b, callback) {
if (a > b) callback(null a + b);
else callback(new Error('a < b'), null);
}
old(5, 3, (err, result) => {
if (err) console.log(err);
else console.log(result);
});
// промиси
function newStyle(a, b) {
return new Promise((resolve, reject) => {
if (a > b) resolve(a + b)
else reject(new Error('a < b'));
});
}
newStyle(5, 3).then(result => {
console.log(result);
}).catch(err => {
console.log(err);
})