I did some i/o function like load yaml configuration file and callback function will return a JSON format of yaml. Is possible return value without callback like below?
var u=require('./my_util');
var oData=u.yaml2json('path/to/yaml');
my_util.js
module.exports={
yaml2json : function(path, callback){
env = process.env.NODE_ENV || 'development';
var fs = require('fs'),
yaml = require('js-yaml');
data=fs.readFileSync(path);
try {
yaml.loadAll(data, function (doc) {
callback(null, doc[env]);
});
} catch(e) {
console.log(e);
}
}
};
callback usage
var u=require('./my_util');
u.yaml2json('path/to/yaml', function(err, oData){
// do something
});
Try the following example from the js-yaml github page…
You can change this into…
PS. Haven’t tested it myself.