-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2776-ConvertCallbackBasedFunctionToPromiseBasedFunction.js
More file actions
95 lines (83 loc) · 3.28 KB
/
2776-ConvertCallbackBasedFunctionToPromiseBasedFunction.js
File metadata and controls
95 lines (83 loc) · 3.28 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 2776. Convert Callback Based Function to Promise Based Function
// Write a function that accepts another function fn and converts the callback-based function into a promise-based function.
// The function fn takes a callback as its first argument,
// along with any additional arguments args passed as separate inputs.
// The promisify function returns a new function that should return a promise.
// The promise should resolve with the argument passed as the first parameter of the callback when the callback is invoked without error,
// and reject with the error when the callback is called with an error as the second argument.
// The following is an example of a function that could be passed into promisify.
// function sum(callback, a, b) {
// if (a < 0 || b < 0) {
// const err = Error('a and b must be positive');
// callback(undefined, err);
// } else {
// callback(a + b);
// }
// }
// This is the equivalent code based on promises:
// async function sum(a, b) {
// if (a < 0 || b < 0) {
// throw Error('a and b must be positive');
// } else {
// return a + b;
// }
// }
// Example 1:
// Input:
// fn = (callback, a, b, c) => {
// callback(a * b * c);
// }
// args = [1, 2, 3]
// Output: {"resolved": 6}
// Explanation:
// const asyncFunc = promisify(fn);
// asyncFunc(1, 2, 3).then(console.log); // 6
// fn is called with a callback as the first argument and args as the rest.
// The promise based version of fn resolves a value of 6 when called with (1, 2, 3).
// Example 2:
// Input:
// fn = (callback, a, b, c) => {
// callback(a * b * c, "Promise Rejected");
// }
// args = [4, 5, 6]
// Output: {"rejected": "Promise Rejected"}
// Explanation:
// const asyncFunc = promisify(fn);
// asyncFunc(4, 5, 6).catch(console.log); // "Promise Rejected"
// fn is called with a callback as the first argument and args as the rest. As the second argument, the callback accepts an error message, so when fn is called, the promise is rejected with a error message provided in the callback. Note that it did not matter what was passed as the first argument into the callback.
// Constraints:
// 1 <= args.length <= 100
// 0 <= args[i] <= 10^4
/**
* @param {Function} fn
* @return {Function<Promise<number>>}
*/
var promisify = function(fn) {
return async function(...args) {
return new Promise((resolve, reject) => {
fn((result, error) => {
if (error) {
reject(error); // 如果回调中有错误,拒绝 Promise
} else {
resolve(result); // 否则,解析 Promise
}
}, ...args);
});
}
};
/**
* const asyncFunc = promisify(callback => callback(42));
* asyncFunc().then(console.log); // 42
*/
let asyncFunc = promisify(callback => callback(42));
asyncFunc().then(console.log); // 42
let fn = (callback, a, b, c) => {
callback(a * b * c);
}
asyncFunc = promisify(fn);
asyncFunc(1, 2, 3).then(console.log); // 6
fn = (callback, a, b, c) => {
callback(a * b * c, "Promise Rejected");
}
asyncFunc = promisify(fn);
asyncFunc(4, 5, 6).catch(console.log); // "Promise Rejected"