Asynchronous Template Helpers #667

Open
yazla opened this Issue Oct 5, 2013 · 6 comments

Comments

Projects
None yet
2 participants
@yazla

yazla commented Oct 5, 2013

Hi, i didn't find a proper place to put this question, so please point me if i choose the wrong one.

The problem is that i didn't find a method to use the async functions in the template helpers. Is this possible at all? I just want to put there the app logic which usually require an async db(Mongo) access.

Thanks.


Want to back this issue? Place a bounty on it! We accept bounties via Bountysource.

@balupton

This comment has been minimized.

Show comment Hide comment
@balupton

balupton Oct 5, 2013

Member

It is quite complicated, however the text and partials plugins do this successfully by these steps:

  1. Spurting out a random number or whatever.
  2. Performing the asynchronous stuff in the background while rendering continues,
  3. In renderAfter or whatever, wait for the async tasks to complete, then replace the random numbers with the results.
Member

balupton commented Oct 5, 2013

It is quite complicated, however the text and partials plugins do this successfully by these steps:

  1. Spurting out a random number or whatever.
  2. Performing the asynchronous stuff in the background while rendering continues,
  3. In renderAfter or whatever, wait for the async tasks to complete, then replace the random numbers with the results.
@yazla

This comment has been minimized.

Show comment Hide comment
@yazla

yazla Oct 5, 2013

Thanks for the answer (and for docpad as well).
Are there any plans to support async template helpers in future? It seems like a very useful feature.

yazla commented Oct 5, 2013

Thanks for the answer (and for docpad as well).
Are there any plans to support async template helpers in future? It seems like a very useful feature.

@balupton

This comment has been minimized.

Show comment Hide comment
@balupton

balupton Oct 10, 2013

Member

The problem isn't really about adding support for asynchronous template helpers, but more about finding asynchronous templating engines. This is because it is traditionally impossible to have asynchronous code executed by a synchronous function, and when the majority of templating engines are synchronous, there is not much we can do.

To illustrate this, take the following example with eco.

Here I execute an asynchronous template helper:
<% @getSomeContentAsynchronously(whatShouldThisCallbackDo?) %>

Perhaps ES6's yield keyword could come to the rescue here:

Here I execute an asynchronous template helper:
<%- yield @getSomeContentAsynchronously() %>

Or just supporting asynchronous templating engines like dust (#401).

Happy to hear some ideas on this.

Member

balupton commented Oct 10, 2013

The problem isn't really about adding support for asynchronous template helpers, but more about finding asynchronous templating engines. This is because it is traditionally impossible to have asynchronous code executed by a synchronous function, and when the majority of templating engines are synchronous, there is not much we can do.

To illustrate this, take the following example with eco.

Here I execute an asynchronous template helper:
<% @getSomeContentAsynchronously(whatShouldThisCallbackDo?) %>

Perhaps ES6's yield keyword could come to the rescue here:

Here I execute an asynchronous template helper:
<%- yield @getSomeContentAsynchronously() %>

Or just supporting asynchronous templating engines like dust (#401).

Happy to hear some ideas on this.

@yazla

This comment has been minimized.

Show comment Hide comment
@yazla

yazla Oct 10, 2013

I was thinking that plugin like partials but more general, would be enough for this task.
Could look like this: (Jade)

.cls
      async('name_of_the_template_helper')

This could work the same way as partials, but with some convention about the callback args.
Of course it doesn't look as elegant as it could be with a separate async template engine, but this is simple and understandable enough i think.

yazla commented Oct 10, 2013

I was thinking that plugin like partials but more general, would be enough for this task.
Could look like this: (Jade)

.cls
      async('name_of_the_template_helper')

This could work the same way as partials, but with some convention about the callback args.
Of course it doesn't look as elegant as it could be with a separate async template engine, but this is simple and understandable enough i think.

@balupton

This comment has been minimized.

Show comment Hide comment
@balupton

balupton Oct 10, 2013

Member

So provide a streamlined and bundled way of accomplishing the "spurt out a placeholder, then replace afterwards" way mentioned earlier?

Member

balupton commented Oct 10, 2013

So provide a streamlined and bundled way of accomplishing the "spurt out a placeholder, then replace afterwards" way mentioned earlier?

@yazla

This comment has been minimized.

Show comment Hide comment
@yazla

yazla Oct 10, 2013

I like such approach. Would be nice.

yazla commented Oct 10, 2013

I like such approach. Would be nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment