Callback to Yieldable Function Converter
Convert Node.js callback-style functions to yieldable thunks or Promises. Supports co, koa patterns and util.promisify output.
About
Node.js callback conventions follow the error-first pattern: the last argument is a function receiving err and result. Generator-based control flow libraries such as co expect yieldable values - thunks or Promises. Manually wrapping every callback function is tedious and error-prone. A missed error branch or a swapped argument index causes silent failures that surface only at runtime under load. This tool parses your callback-style function signatures and emits wrapped versions as thunks (single-arity functions returning function(done)) or as Promises via explicit constructor or util.promisify output. It handles arrow functions, rest parameters, and default values. The conversion is purely structural - it does not execute your code. Limitation: deeply nested or dynamically constructed callbacks cannot be detected by static pattern matching.
Formulas
A thunk is a function of arity 1 that accepts a single done callback. The conversion identity for an error-first callback function f with n arguments plus callback:
The Promise conversion follows the constructor pattern:
Where f is the original callback-style function, a1 through an are the non-callback arguments, done is the thunk consumer callback, err follows the Node.js error-first convention, and res is the success value. The util.promisify output delegates to Node's internal implementation which additionally respects util.promisify.custom symbols.
Reference Data
| Pattern | Signature | Yieldable? | Output Style | co Compatible | async/await Compatible |
|---|---|---|---|---|---|
| Error-first callback | fn(arg, cb(err, res)) | No (raw) | Thunk / Promise | After wrap | After wrap |
| Thunk | fn(arg) → function(done) | Yes | Native | Yes | No |
| Promise | fn(arg) → Promise | Yes | Native | Yes | Yes |
| util.promisify | util.promisify(fn) | Yes | Promise | Yes | Yes |
| Node stream | EventEmitter-based | No | Needs wrapper lib | No | No |
| Synchronous | fn(arg) → value | Yes (trivially) | Already yieldable | Yes | Yes |
| Multi-callback | fn(onSuccess, onError) | No | Custom wrap needed | No | No |
| Continuation-passing | fn(arg, k) | No | Thunk-convertible | After wrap | After wrap |
| Bluebird promisifyAll | Bulk conversion | Yes | Promise | Yes | Yes |
| cb-style with context | obj.method(arg, cb) | No | Needs .bind | After bind+wrap | After bind+wrap |
| Nested callbacks | Callback inside callback | No | Flatten first | No | No |
| Event + callback hybrid | Mixed patterns | No | Manual refactor | No | No |