Можете переписать код на async/await
const { parseStringPromise } = require('xml2js');
const fs = require('fs');
const { promisify } = require('util');
const readFile = promisify(fs.readFile);
(async () => {
const data = await readFile('data.xml');
const result = await parseStringPromise(data);
const starting = ("" + result['index']['starting']);
// your code
console.log(starting);
});
или
const parser = new xml2js.Parser();
let starting;
function runMyCode() {
// your code
console.log(starting);
}
fs.readFile('data.xml', function (data) {
parser.parseString(data, function (result) {
starting = ("" + result['index']['starting']);
runMyCode();
})
})