fastify/avvio

Return avvio inside after breaks avvio

Closed this issue · 0 comments

If you use avvio#next, there is a rather funny bug, if you return avvio inside an after function without callback (I guess it will happens the same inside a use), avvio will go crazy and stop its execution.

'use strict'

const avvio = require('.')()

avvio.use((instance, opts, next) => {
  console.log('plugin 1')
  next()
})

avvio.after(() => {
  console.log('after plugin 1')
  return avvio // <=================== LOOK HERE
})

avvio.use((instance, opts, next) => {
  console.log('plugin 2')
  next()
})

avvio.ready(err => {
  console.log(err || 'ready')
})

/*

Actual log:

plugin 1
after plugin 1



Expected log:

plugin 1
after plugin 1
plugin 2
ready

*/

This is very likely caused by how we are handling promises and the fact that avvio itself is a thenable.
https://github.com/mcollina/avvio/blob/5d36884e9f2c4483a23466668a41a4edb9e9ff4c/boot.js#L434-L451

cc @davidmarkclements