Is there a way to run app.render asynchronously?
I tried promisifying it using bluebird but I get an error
let render = Promise.promisify(req.app.render);
html = await render('email', { email: '[email protected]' });
output:
TypeError: Cannot read property 'cache' of undefined
https://github.com/expressjs/express/blob/3ed5090ca91f6a387e66370d57ead94d886275e1/lib/response.js#L973-L981 - I think the problem is that render is trying to send back the response. So even if you did manage to delay the render method and do it later on, you'd get an error saying headers already sent. However, if you read the comments of the render function in the link i provided, you can pass a callback to stop the response being done automatically. I hope this helps. Try passing a false boolean to the cache as well
Okay, that link points to res.render instead of app.render, but it pointed my into the right direction.
When I made render into it's own variable, it lost the reference to this, so in order to fix it, you would
const Promise = require('bluebird');
req.app.renderAsync = Promise.promisify(req.app.render);
let html = await req.app.renderAsync('email', { email: '[email protected]' });
That actually works, thanks @fredski02
Most helpful comment
Okay, that link points to
res.renderinstead ofapp.render, but it pointed my into the right direction.When I made
renderinto it's own variable, it lost the reference tothis, so in order to fix it, you wouldThat actually works, thanks @fredski02