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

How can I register async helpers? #168

Open
MacgyverMartins opened this issue Jan 27, 2016 · 3 comments
Open

How can I register async helpers? #168

MacgyverMartins opened this issue Jan 27, 2016 · 3 comments

Comments

@MacgyverMartins
Copy link

First of all, sorry for my bad english.
I need write a helper that do a query on mongodb. This helper should be async, like de registerAsyncHelper from express-hbs:

hbs.registerAsyncHelper('readFile', function(filename, cb) {
  fs.readFile(path.join(viewsDir, filename), 'utf8', function(err, content) {
    cb(new hbs.SafeString(content));
  });
});

How can I do this with express-handlebars?
I try return a promise, but it's not work.
I am beginner with node and handlebars, so sorry if this is a stupid question.

@dynnamitt
Copy link

👍

@tndev
Copy link

tndev commented Mar 25, 2016

The "problem" is most likely that Handlebars itself does not support async helper (feature suggestion | Synchronous/Asynchronous Helpers #717), the ability to have an async helper in express-hbs is a hack on top of handlebars.

@s-stude
Copy link

s-stude commented Apr 17, 2023

For those who still might be interested:

your HTML

{{renderAsync 'some context'}}

your JS

hbs.registerAsyncHelper( 'renderAsync', async function renderAsync( context, helperFn, resolver ) {
          // await doAsyncStuff();
          resolver( `<p> rendered HTML </p>` )
      }
  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants