EasyPython

Callback → Coroutine Adapter

PythonAsyncasyncio

Some APIs use error-first callbacks: callback(err, result=None).
Implement promisify(fn) that wraps such a function into one you can
await, using an asyncio.Future — the same idea as Node's util.promisify.

await promisify(fn)(...args) should:

  • Resolve with result if the callback is invoked as callback(None, result).
  • Raise the error if the callback is invoked as callback(err).

solve drives your implementation through a series of test cases and
returns whether each resolved or raised.

Sample tests

Test #1Falsy value (0) still resolves
Input: [[{"fail":false,"value":0}]]
Output: [{"ok":true,"val":0}]
Test #2Boolean values
Input: [[{"fail":false,"value":true},{"fail":false,"value":false}]]
Output: [{"ok":true,"val":true},{"ok":true,"val":false}]
Test #3Resolve with value
Input: [[{"fail":false,"value":42}]]
Output: [{"ok":true,"val":42}]
Test #4Reject on error
Input: [[{"fail":true,"value":null}]]
Output: [{"ok":false}]
Test #5Mixed resolve and reject
Input: [[{"fail":false,"value":"hello"},{"fail":true,"value":null}]]
Output: [{"ok":true,"val":"hello"},{"ok":false}]