-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromisify.js
More file actions
40 lines (38 loc) · 864 Bytes
/
Promisify.js
File metadata and controls
40 lines (38 loc) · 864 Bytes
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
/**
* @Description: 将基于回调的函数转换为基于Promise 的函数
* @Author: oceanhhan
* @Date: 2025-12-18 11:23:28
*/
function promisify(func) {
return function (...args) {
return new Promise((resolve, reject) => {
func.apply(this, [...args, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
}]);
});
};
}
// 测试代码
function func(a, b, callback) {
console.log(this.name); // oceanhhan
setTimeout(() => {
if (a < 0 || b < 0) {
callback(new Error('参数不能为负数'));
} else {
callback(null, a + b);
}
}, 1000);
}
const obj = {
name: 'oceanhhan',
promisedFn: promisify(func)
};
obj.promisedFn(1, 2).then(result => {
console.log('结果:', result); // 结果: 3
}).catch(err => {
console.error('错误:', err);
});