Open gnought opened 4 years ago
Every time there is a function that is user specified, we should wrap it in the following way:
const result = fn(done)
if (result && typeof result.then === 'function') {
result.then(function (res) { done(null, res) }, done)
}
This might become more specific in certain cases, see https://github.com/fastify/fastify/blob/master/lib/wrapThenable.js for an example
what about create an helper executor? Something like:
async function runSafe(fn) {
if (fn) {
return typeof fn.then === 'function' ? await fn() : fn()
}
return null
}
unfortunately it won't work. It cannot be an async function otherwise you can get into problems of handling errors.
@mcollina Should we handle errors even in functions given by users? For example authenticate
? Shouldn't that be handled by them?
In order to be reliable, we need to handle the errors thrown by async
functions as they are errors passed to the cb
.
Lots of aedes codes are in callback-based, is it possible to switch to promise-based?
That will slow down aedes significantly, as well as increasing memory load.
Please suggest preferable ways to make aedes-codebase be promise friendly?