Answer the question
In order to leave comments, you need to log in
How to use promise for this code?
var xml_file = fs.readFileSync('xml/jp/eshop_jp.xml', 'utf8');
xml2js(xml_file, function (err, result) {
fs.writeFile('json/jp/eshop_jp.json', JSON.stringify(result,"",2), (err) => {
if (err) {
console.error(err);
return;
};
});
});
var eshop_jp = JSON.parse(fs.readFileSync('json/jp/eshop_jp.json', 'utf8'));
Answer the question
In order to leave comments, you need to log in
like this
function readXmlFIle(file, jsonfile) {
return new Promise(function(resolve, reject){
var xml_file = fs.readFileSync(file, 'utf8');
xml2js(xml_file, function (err, result) {
if(err){
reject(err);
return;
}
fs.writeFile(jsonfile, JSON.stringify(result,"",2), function(error){
if (error) {
reject(error);
return;
};
resolve();
});
});
});
}
readXmlFIle('xml/jp/eshop_jp.xml', 'json/jp/eshop_jp.json').then(function(){
var eshop_jp = JSON.parse(fs.readFileSync('json/jp/eshop_jp.json', 'utf8'));
}).catch(function(err){
console.log(err)
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question