Imt*_*ury 2 ejs node.js express async-await
我想从我的 ejs 文件中调用我的一些异步函数。
就像我将此功能设置为我的 app.locals.someFunction
async someFunction(param){
let data = await someAsyncStuff();
// & other things
return data;
}
Run Code Online (Sandbox Code Playgroud)
我想在 ejs 文件中使用它,如下所示:
<%
let data = await someFunction()
for(let x of data){%>
<li><%=x%></li>
<%}%>
Run Code Online (Sandbox Code Playgroud)
如果{async:true}作为选项传递,这对于 ejs 是可能的。但是,当我的视图引擎设置如下所示时,我到底应该在哪里传递它?
//view engine setup
app.engine ('.html', ejs.renderFile);
app.set ('view engine', 'html');
Run Code Online (Sandbox Code Playgroud)
而是 res.render()
const ejs = require('ejs');
const html = await ejs.renderFile(view, data, {async: true});
res.send(html);
Run Code Online (Sandbox Code Playgroud)
每个包含等待
<body>
<%- await include(pageView);%>
</body>
Run Code Online (Sandbox Code Playgroud)
异步现在很好
<%
let data = await collection.find().toArray();
for(let x of data){%>
<li><%=x%></li>
<%}%>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5798 次 |
| 最近记录: |