This builds on the Promisify question. Implement promisify(fn) for Node-style callback functions (callback last, called as (error, value)), plus support for a custom promisified implementation, modelled on Node's util.promisify.custom.
Some functions can't be promisified mechanically. For example, a callback might receive several values, or the function might not use the error-first convention. Their authors can attach a hand-written promise version under a well-known symbol, and promisify returns that instead.
// Expose the symbol as a property of promisify:
promisify.custom === Symbol.for('nodejs.util.promisify.custom'); // true
function legacyStat(path, callback) {
callback(null, 1024, 'file'); // two values, which mechanical promisify would truncate
}
legacyStat[promisify.custom] = (path) => Promise.resolve({ size: 1024, type: 'file' });
const stat = promisify(legacyStat);
stat === legacyStat[promisify.custom]; // true: the custom version is returned as-is
await stat('/tmp/a'); // { size: 1024, type: 'file' }
promisify.custom is Symbol.for('nodejs.util.promisify.custom'). Using the global symbol registry means separately bundled code can agree on the symbol without importing it from your module.fn[promisify.custom] is defined:
TypeError.this, and turn synchronous throws into rejections.promisify.custom, so promisifying it again returns the same function: promisify(promisify(f)) === promisify(f).TypeError if fn is not a function.Symbol.for rather than Symbol()?Spotted a mistake or a rights concern? Report an issue