aedes icon indicating copy to clipboard operation
aedes copied to clipboard

Ways to promisify aedes-codebase friendly.

Open gnought opened this issue 5 years ago • 7 comments

Please suggest preferable ways to make aedes-codebase be promise friendly?

gnought avatar Jan 30 '20 16:01 gnought

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

mcollina avatar Jan 31 '20 10:01 mcollina

what about create an helper executor? Something like:

async function runSafe(fn) {

if (fn) {
 return  typeof fn.then === 'function' ? await fn() : fn()
}

return null

}

robertsLando avatar Jan 31 '20 10:01 robertsLando

unfortunately it won't work. It cannot be an async function otherwise you can get into problems of handling errors.

mcollina avatar Jan 31 '20 11:01 mcollina

@mcollina Should we handle errors even in functions given by users? For example authenticate ? Shouldn't that be handled by them?

robertsLando avatar Jan 31 '20 13:01 robertsLando

In order to be reliable, we need to handle the errors thrown by async functions as they are errors passed to the cb.

mcollina avatar Feb 01 '20 10:02 mcollina

Lots of aedes codes are in callback-based, is it possible to switch to promise-based?

gnought avatar Feb 01 '20 19:02 gnought

That will slow down aedes significantly, as well as increasing memory load.

mcollina avatar Feb 01 '20 23:02 mcollina