Skip to content

Commit

Permalink
Added Promise support with capabilities test to renderFile
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Jan 5, 2018
1 parent 8c575c9 commit c04ec96
Showing 1 changed file with 42 additions and 18 deletions.
60 changes: 42 additions & 18 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,32 @@ function handleCache(options, template) {

function tryHandleCache(options, data, cb) {
var result;
try {
result = handleCache(options)(data);
if (!cb) {
if (typeof Promise == 'function') {
return new Promise(function (resolve, reject) { // eslint-disable-line no-undef
try {
result = handleCache(options)(data);
resolve(result);
}
catch (err) {
reject(err);
}
});
}
else {
throw new Error('Please provide a callback function');
}
}
catch (err) {
return cb(err);
else {
try {
result = handleCache(options)(data);
}
catch (err) {
return cb(err);
}

cb(null, result);
}
return cb(null, result);
}

/**
Expand Down Expand Up @@ -399,17 +418,27 @@ exports.render = function (template, d, o) {
*/

exports.renderFile = function () {
var filename = arguments[0];
var cb = arguments[arguments.length - 1];
var args = Array.prototype.slice.call(arguments);
var filename = args.shift();
var cb;
var opts = {filename: filename};
var data;

if (arguments.length > 2) {
data = arguments[1];

// No options object -- if there are optiony names
// in the data, copy them to options
if (arguments.length === 3) {
// Do we have a callback?
if (typeof arguments[arguments.length - 1] == 'function') {
cb = args.pop();
}
// Do we have data/opts?
if (args.length) {
// Should always have data obj
data = args.shift();
// Normal passed opts (data obj + opts obj)
if (args.length) {
// Use shallowCopy so we don't pollute passed in opts obj with new vals
utils.shallowCopy(opts, args.pop());
}
// Special casing for Express (opts-in-data)
else {
// Express 4
if (data.settings) {
if (data.settings.views) {
Expand All @@ -424,11 +453,6 @@ exports.renderFile = function () {
utils.shallowCopyFromList(opts, data, _OPTS_EXPRESS);
}
}
else {
// Use shallowCopy so we don't pollute passed in opts obj with new vals
utils.shallowCopy(opts, arguments[2]);
}

opts.filename = filename;
}
else {
Expand Down

0 comments on commit c04ec96

Please sign in to comment.