-
Notifications
You must be signed in to change notification settings - Fork 1
/
asyncDivision.js
34 lines (31 loc) · 968 Bytes
/
asyncDivision.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
// 定义一个异步的触发运算,使得可以按照传回调或者then的方法同时支持调用
// 如:
// callback oriented usage
asyncDivision(10, 2, (error, result) => {
if (error) {
return console.error(error);
}
console.log(result);
});
// promise oriented usage
asyncDivision(22, 11)
.then(result => console.log(result))
.catch(error => console.error(error));
module.exports = function asyncDivision(dividend, divisor, cb) {
return new Promise((resolve, reject) => {
process.nextTick(() => {
const result = dividend / divisor;
if (isNaN(result) || !Number.isFinite(result)) {
const error = new Error('Invalid operands');
if (cb) {
cb(error);
}
return reject(error);
}
if (cb) {
cb(null, result);
}
resolve(result);
});
});
};