JavaScript How to run parseString() function synchronously in Javascript node.js

I have this code, where I run parseString()to extract some information from an xml file

function parseTime(){

var parser = new xml2js.Parser();
var data = fs.readFileSync('C:\\Temp\\tasks\\acis\\110-1100.sat\\110-1100.sat.response.xml', {encoding:'utf8'});

parser.parseString(data, function (err, result) {

var timeString = result.Message.Response[0].Events[0].MessageReportEvent[8].$.Message;
var fileTime = timeString.substr(13,20);
var filetimeVal = parseFloat(fileTime);

console.log(filetimeVal);

return filetimeVal;

});

};

What changes should I do to run parseString synchronously or is there a way to extract the xml data via a deifferent synchronous method

Answer:1

The callback of the parseString already runs synchronously. Your program would reach the parsed results before it reaches any other line of code.

There is async switch which is false by default in the options.

However the developers warn that this default might change in the future.

async (default false): Should the callbacks be async? This might be an incompatible change if your code depends on sync execution of callbacks. Future versions of xml2js might change this default, so the recommendation is to not depend on sync execution anyway. Added in 0.2.6.

Answer:2

I'm sure this is a duplicate, but I couldn't find the right search terms to find an answer. I'm trying to use hasOwnProperty() to determine if a function exists on an object or not. I know there are ...

I'm sure this is a duplicate, but I couldn't find the right search terms to find an answer. I'm trying to use hasOwnProperty() to determine if a function exists on an object or not. I know there are ...

I'm using the bootstrap-datetimepicker in a form with a radio button, and I'm having a lot of issues with it. The first problem is that only the old version of it is working for me, not the current ...

I'm using the bootstrap-datetimepicker in a form with a radio button, and I'm having a lot of issues with it. The first problem is that only the old version of it is working for me, not the current ...

  1. bootstrap datetimepicker layout
  2. bootstrap datepicker layout

How is it possible to implement, using CSS only (the worst of cases using some JS) to do the following global.css (readonly) .red { color: red; /*tens of lines of additionnal css*/ } ...

How is it possible to implement, using CSS only (the worst of cases using some JS) to do the following global.css (readonly) .red { color: red; /*tens of lines of additionnal css*/ } ...

  1. conditionally apply class angular
  2. conditionally apply class react
  3. conditionally apply class vue
  4. conditionally apply class angular 4
  5. conditionally apply class angularjs
  6. conditionally apply class
  7. conditionally apply class in angular2
  8. conditionally add class react
  9. conditionally add class angular
  10. conditionally add classname react
  11. conditionally add class vue
  12. conditionally add class angular 6
  13. conditionally add class angular 2
  14. conditionally add class angular 4
  15. css conditionally apply class
  16. react conditionally apply classname
  17. vuejs conditionally apply class
  18. conditionally apply css class angular
  19. razor conditionally apply class
  20. add conditionally class

I have a form that is used to update a record, at the bottom I have a submit button. I want it to display a messaging saying "Update Record:" and a Yes and No box. Every way I've found is just for ...

I have a form that is used to update a record, at the bottom I have a submit button. I want it to display a messaging saying "Update Record:" and a Yes and No box. Every way I've found is just for ...

  1. submit button display text
  2. submit button display none
  3. submit button display block
  4. submit button display as link