callbackify or promise-nodeify #2506
Description
🚨 Bikeshed warning 🚨
In #2390 I reached for callbackify
when converting the internals to promises (only partially because the name has a nice symmetry with promisify
), but missed that we already had promise-nodeify
in the codebase.
It seems silly to have both when they do similar things. Ordinarily I'd just swap out callbackify
but I'm not a great fan of how promise-nodeify
makes you add boilerplate to handle setting up the promise yourself (I think, I could be using it wrong) and prefer how callbackify
lets you write an async
function and forget callbacks are even a thing.
The caveat is that your callbackified
function must return a promise - if you return a value instead everything explodes which leads to a few functions marked async
superfluously, but I don't think this is a big deal as we've sort of agreed to mark promise returning functions async
even if they just chain through to a different function that actually does some async work.
Given:
async function returnsPromise (options) {
// do some async stuff
}
callbackify
const func = callbackify.variadic(returnsPromise)
promise-nodeify
const func = (options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
return promiseNodeify(returnsPromise(options), callback)
}
@hugomrdias you started to have an objection to callbackify
in the review of #2390 - could you explain a bit here?