question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use Asynchronous functions in EJS templates?

See original GitHub issue

How to handle asynchronous function calls in the template?

I want to fetch some data (e.g. from GitHub API) and render them in the template.

Is it possible to do something like this?

<%
 fetchData(..., function (err, data) { %>
   <%- err ? "Error" : data.title %>
<% }); %>

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
SystemDisccommented, Mar 6, 2017

I think in almost all cases, you should do any asynchronous stuff before you render your EJS string.

Something similar to this:

doAsyncStuff().then((data) => {
    let rendered = ejs.render(templateString, data);
});
1reaction
IonicaBizaucommented, Jun 20, 2016

@TimothyGu @RyanZim To have an ejs like syntax but with async support, what should I do? Is it possible to build it on top of ejs or should I parse everything from scratch again, like in ajs?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ejs async true with node Express
For people who are still trying to figure out how to implement async ejs templates, I am listing my complete solution below:.
Read more >
EJS -- Embedded JavaScript templates
async When true , EJS will use an async function for rendering. (Depends on async/await support in the JS runtime. Tags. <% 'Scriptlet'...
Read more >
async-ejs
ejs with the ability to add asynchronous functions. ... Start using async-ejs in your project by running `npm i ... Template stuff here....
Read more >
How to use the ejs.renderFileAsync function in ejs
To help you get started, we've selected a few ejs examples, based on popular ways it is used in public projects. ; FirstLegoLeague...
Read more >
ejs.render JavaScript and Node.js code examples
Best JavaScript code snippets using ejs.render(Showing top 15 results out of 315) ... async function buildReadmeContent(context) { const template = await ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found