Last active
August 29, 2015 14:17
-
-
Save oriSomething/4361cfe18ff03036a02a to your computer and use it in GitHub Desktop.
use promises for template's data in Express.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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