Make res.render callback is always async, even for sync view engines

closes #2668
This commit is contained in:
Douglas Christopher Wilson 2015-07-07 01:02:13 -04:00
parent 694869d2aa
commit 6343288bef
2 changed files with 25 additions and 1 deletions

View File

@ -7,6 +7,7 @@ This incorporates all changes after 4.10.1 up to 4.13.1.
- `app.param(fn)`
- `req.param()` -- use `req.params`, `req.body`, or `req.query` instead
* change:
- `res.render` callback is always async, even for sync view engines
- The leading `:` character in `name` for `app.param(name, fn)` is no longer removed
- Use `router` module for routing
- Use `path-is-absolute` module for absolute path detection

View File

@ -122,8 +122,31 @@ View.prototype.lookup = function lookup(name) {
*/
View.prototype.render = function render(options, callback) {
var sync = true;
debug('render "%s"', this.path);
this.engine(this.path, options, callback);
// render, normalizing sync callbacks
this.engine(this.path, options, function onRender() {
if (!sync) {
return callback.apply(this, arguments);
}
// copy arguments
var args = new Array(arguments.length);
var cntx = this;
for (var i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
}
// force callback to be async
return process.nextTick(function renderTick() {
return callback.apply(cntx, args);
});
});
sync = false;
};
/**