Promisify a Callback Function
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write promisify(fn), where fn is a Node-style function: its last argument is a callback, which it calls once with (error, value). Return a function that takes fn's other arguments and returns a promise. It calls fn with those arguments plus a callback; the promise rejects with error if error is anything other than null or undefined, and otherwise resolves with value. The returned function calls fn with the same this it was called with, so it also works as a method.
Your code must pass
- Resolves with the value passed to the callback
- Rejects with the error passed to the callback
- Passes the arguments through, with the callback last
- Keeps this when used as a method