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');
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);
}
}
};
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...
var yaml = require('js-yaml');
// pass the string
fs.readFile('/home/ixti/example.yml', 'utf8', function (err, data) {
if (err) {
// handle error
return;
}
try {
console.log( yaml.load(data) );
} catch(e) {
console.log(e);
}
});
You can change this into...
var yaml = require('js-yaml');
// pass the string
var data = fs.readFileSync('/home/ixti/example.yml');
try {
console.log( yaml.load(data) );
} catch(e) {
console.log(e);
}
PS. Haven't tested it myself.