diff --git a/lib/ejs.js b/lib/ejs.js index e8fa21ba..a23fe69f 100755 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -641,7 +641,7 @@ Template.prototype = { e.message += ' while compiling ejs\n\n'; e.message += 'If the above error is not helpful, you may want to try EJS-Lint:\n'; e.message += 'https://github.com/RyanZim/EJS-Lint'; - if (!e.async) { + if (!opts.async) { e.message += '\n'; e.message += 'Or, if you meant to create an async function, pass async: true as an option.'; } @@ -905,12 +905,25 @@ exports.escapeXML = utils.escapeXML; * Express.js support. * * This is an alias for {@link module:ejs.renderFile}, in order to support - * Express.js out-of-the-box. + * Express.js out-of-the-box. If rendered using async, a wrapper callback + * is passed to {@link module:ejs.renderFile} that waits until the returned + * promise is resolved and passes the result to Express.js * * @func */ -exports.__express = exports.renderFile; +exports.__express = function (file, opts, callback) { + if (opts.async) { + exports.renderFile(file, opts, function (err, promise) { + promise.then(function (data) { + callback(err, data); + }); + }); + } + else { + exports.renderFile(file, opts, callback); + } +}; // Add require support /* istanbul ignore else */