javascript - Wait until processing completes before exporting Node.js module? -
i'm loading configuration object, remapping properties in for..in loop below (it string concatenation on values of .styles array of strings). when i'm requiring module, re-mapping hasn't occurred. i'm guessing sort of async issue, i'm not sure how handle it. suggestions?
// init vars var buildconfig = require('./build-config.js'); var csspath = buildconfig.paths.csspath; var bundle = buildconfig.bundle; // callbacks var setpathsgeneric = function(basepath, extname) { // currying function return function(val, index, self) { return basepath + val + extname; }; }; var setcsspaths = setpathsgeneric(csspath, '.css'); // map key values actual url paths (var key in bundle) { if(bundle[key].styles && bundle[key].styles.length) { bundle[key].styles.map(setcsspaths); } } module.exports = { bundle: bundle };
when i'm requiring module, re-mapping hasn't occurred. i'm guessing sort of async issue, i'm not sure how handle it.
your code 100% synchronous, node executes "re-mapping" code synchronously before exporting bundle object.
i think, problem array.prototype.map() not modify array, returns new array instead. so, should assign new value bundle[key].styles yourself:
bundle[key].styles = bundle[key].styles.map(setcsspaths);
Comments
Post a Comment