Skip to content
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

Refactoring to support async/await #87

Closed
mcollina opened this issue Dec 12, 2018 · 5 comments
Closed

Refactoring to support async/await #87

mcollina opened this issue Dec 12, 2018 · 5 comments
Labels

Comments

@mcollina
Copy link
Member

I think it's due that we improve the support for async/await in this module.

Currently, it's not working as expected mainly because loading templates could be asynchronous, and that does not work well with the internal call to .send(), as there is no way to await that.

It following should work with any template engine.

What we need as next steps is a proposal to implement an API that could be awaited upon, and then an implementation.

@jamilservicos
Copy link

i use:

return await app.view("template/index", {teste: "hello"}).then(html => {
                    return reply.code(200)
                        .header('Content-Type', 'text/html; charset=utf-8')
                        .send(html);
                });

@mcollina
Copy link
Member Author

mcollina commented Feb 1, 2019

That's a good one @jamilservicos!

It's the reply.view  one that's not working well IMHO.

Note that I would prefer the following:

const html = await app.view("template/index", {teste: "hello"})
reply.code(200).header('Content-Type', 'text/html; charset=utf-8')
return html

@jamilservicos
Copy link

I use this my shortcut in a plugin just passing:

await app.render("template/index", {teste: "hello"}, reply);

plugin:

fastify.decorate('render', async (view, options, reply) => {
       return await fastify.view(view, options).then(html => {
            return reply.code(200)
                .header('Content-Type', 'text/html; charset=utf-8')
                .send(html);
        });

@Hamper
Copy link

Hamper commented Feb 19, 2019

I'm just rewite previous decorator in async style:

fastify.decorate('render', async (view, options, reply) => {
    return reply.code(200)
        .header('Content-Type', 'text/html; charset=utf-8')
        .send(await fastify.view(view, options));
});

@stale
Copy link

stale bot commented Oct 21, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 21, 2020
@stale stale bot closed this as completed Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants