Skip to content

Instantly share code, notes, and snippets.

@oriSomething
Last active August 29, 2015 14:17
Show Gist options
  • Save oriSomething/4361cfe18ff03036a02a to your computer and use it in GitHub Desktop.
Save oriSomething/4361cfe18ff03036a02a to your computer and use it in GitHub Desktop.
use promises for template's data in Express.js
import _ from 'lodash';
function makeStatic(data) {
return Promise.all(_.map(data, function map(v, k) {
if (v instanceof Promise) {
return v.then(x => x, () => null)
.then(data => [k, data]);
}
return [k, v];
})).then(function success(values) {
return _.reduce(values, (x, y) => _.assign(x, { [y[0]]: y[1] }), {});
});
}
/* ... */
app.use('/', function route(req, res) {
/* ... */
makeStatic(data).then(data => res.render('index', data));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment