EasyPythonJavaScriptTypeScript
Implement promisify()
BackendAsync
Node.js core uses error-first callbacks: (err, result) => void.
Implement promisify(fn) that wraps such a function to return a Promise.
promisify(fn)(...args) should:
- Resolve with
resultif the callback is called with(null, result). - Reject with the
errorif the callback is called with(err).
solve drives your implementation through a series of test cases
and returns whether each resolved or rejected.
Sample tests
Test #1Resolve with value
Input: [[{"fail":false,"value":42}]]
Output: [{"ok":true,"val":42}]
Test #2Reject on error
Input: [[{"fail":true,"value":null}]]
Output: [{"ok":false}]
Test #3Mixed resolve and reject
Input: [[{"fail":false,"value":"hello"},{"fail":true,"value":null}]]
Output: [{"ok":true,"val":"hello"},{"ok":false}]
Test #4Falsy value (0) still resolves
Input: [[{"fail":false,"value":0}]]
Output: [{"ok":true,"val":0}]
Test #5Boolean values
Input: [[{"fail":false,"value":true},{"fail":false,"value":false}]]
Output: [{"ok":true,"val":true},{"ok":true,"val":false}]