Create a function promisify(fn) that converts a Node.js-style callback function into a function that returns a promise.
The difference from the standard version is:
undefined, then the promise-based behavior should take place.(err, result).Examples
function asyncAdd(a, b, cb) { setTimeout(() => cb(null, a + b), 100); } const promisedAdd = promisify(asyncAdd); promisedAdd(2, 3).then(console.log); // 5 function syncReturn(x) { return x * 2; } const wrapped = promisify(syncReturn); console.log(wrapped(4)); // 8, returns directly instead of a Promise
JavaScript Function
No test results yet
Click "Run" to execute tests