-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Weird behavior with fastify.register
, async/await
and point-of-view
#267
Comments
Here's a smaller code to replicate the issue. const fastify = require('fastify')();
const reg = async function(fastify) {
_ = await 1;
fastify.register(require('point-of-view'), {
engine: {
handlebars: require('handlebars')
}
});
}
_ = (async function() {
await reg(fastify);
fastify.get('/', (req, reply) => {
reply.view('/templates/index.hbs', { text: 'text' })
})
fastify.listen(3000)
})(); |
What you are doing is clearly unsupported. I am a bit lost on why you would like to load fastify using async/await, maybe if you give us a little bit more context we can help. We also accept PRs on all our modules. In fact, I think that the next version of avvio (I’ll release that tomirrow), might fix this. |
It's not about loading fastify in async, it's a consequence of trying to make the code cleaner.
They are async because they need to scan directories, get info from db, etc... Basically I have three options - doing everything synchronously, using promises w/ chaining, using async/await. The last one makes code cleaner. Because of this I need to wrap Hope it's clear what are my intentions |
Hi! |
@delvedor, I might have been missing something. Just to clarify, my use-case is something like this.
I picked the second option, as a result my Did I miss something? Because it looks like it can't be done in fastify without droping async/await. Anyway, thanks for response. Will check avvio. P.S. I still don't understand why loading fastify inside async/await is not supported, some tips? |
@iCyberon a problem in your code snippet is that register as a callback, which you are currently ignoring, and it ends up with two competing system (fastify/avvio and async/await) to asynchronously control the startup of the application. This is exercising some code path that we have not tested, and it probably has a bug. If you would like to send a PR to help supporting your use case, we would be very happy. You should wrap your lookup logic of plugins, decorators and middlewares in plugins. Just call |
Closing this, if you have more questions feel free to reopen :) |
I'm having the same issue, I think... Here's my sippet: const registerRoutes = () => new Promise((resolve, reject) => {
app.register(router, {}, (err) => {
if (err) return reject(err);
app.Router.route(routes);
resolve();
});
}); Inside the callback app.Router is undefined, but if I execute it outside of a Promise it behaves normally... any idea why? |
@rodrigogs can you paste also the Inside the callback you can't access |
How not? app.register(router, {}, (err) => {
if (err) throw err;
app.Router.route(routes);
}); This example works perfectly. It just doesn't work when executed inside a promise... wich makes no sense to me. Router code is here: https://github.com/rodrigogs/fastify-router |
I already came with a solution, sinse the promise I was using were useless. But I still wonder why the same code wont work inside a promise. :/ |
Hey, during my initial tests I've encountered a weird issue related to
fastify.register
andasync/await
. Below is a simplified version of what I'm doing.app.js
plugins.js
is a wrapper to register plugins/plugins/views.js
is loaded byplugins.js
This doesn't work and I get an exception
But if I replace
const plugins = await readdir(path.join(__dirname, 'plugins'))
with a sync versionconst plugins = fs.readdirSync(path.join(__dirname, 'plugins'))
everything works as expected.The text was updated successfully, but these errors were encountered: