coolDude February 2016

Synchronous Call : Call 2nd function after 1st function is executed completely

I recently stating coding in node.js and might be a very simple question.

Trying to write a XML parser/validator to validate xml schema and values against values/ xpath stored in an excel sheet.

Now once the validation function is complete I want to call a printResult function to print final result. However if I try to call the function immediately after the first function .. its printing variables initial values and if called within the first which is iterating though the number of xpaths present in excel sheet and printing result with increments.

var mocha = require('mocha');
var assert = require('chai').assert;
var fs = require('fs');
var parseString = require('xml2js').parseString;
var xpath = require('xpath');
var dom = require('xmldom').DOMParser;
var XLSX = require('xlsx');
var Excel = require("exceljs");
var should = require('chai').should();
var HashMap = require('hashmap');
var colors = require('colors');
require('/xmlValidator/dbConnect.js');

var map = new HashMap();
var elementMap = new HashMap();
var resultValue;

//console.log('hello'.green);

map.set("PASS", 0);
map.set("FAIL", 0);
map.set("INVALID_PATH", 0);

function computeResult(elementPath, result) {
  var pass = map.get("PASS");
  var fail = map.get("FAIL");
  var invalidPath = map.get("INVALID_PATH");

  elementMap.set(elementPath, result);

  if (result == "PASS") {
    pass++;
    map.set("PASS", pass);
  } else if (result == "FAIL") {
    fail++;
    map.set("FAIL", fail);
  } else {
    invalidPath++;
    map.set("INVALID_PATH", invalidPath)
  }
  printResult();
}

function printResult() {
  var pass = map.get("PASS");
  var fail = map.get("FAIL");
  var invalidPath = map.get("INVALID_PATH");
  console.log(("PASS Count :" + pass).green);
  console.log        

Answers


Roamer-1888 February 2016

If fs.readFileAsync('utils/' + process.argv[3], 'utf8') can be executed once, then validate() will be completely synchronous and calling printResult() after the verification loop will be trivial.

In the main routine, you can simply aggregate two promises ...

var promise1 = workbook.xlsx.readFile();
var promise2 = fs.readFileAsync(); // requires `fs` to be promisified. 

... before embarking on the verification loop.

Promise.all([promise1, promise2]).spread(/* verify here */);

Also a whole bunch of tidying can be considered, in particular :

  • establishing "PASS", "FAIL" and "INVALID_PATH" as constants to avoid lots of repetitive string creation,
  • using js plain objects in lieu of hashmap,
  • building map from elementMap inside the print function.
  • having validate() return its result and building elementMap in the main routine

Here's the whole thing, about as tight as I can get it. I may have made a few assumptions but hopefully not too many bad ones ...

var mocha = require('mocha');
var assert = require('chai').assert;
var Promise = require("bluebird");
var fs = Promise.promisifyAll(require("fs")); // allow Bluebird to take the pain out of promisification.
var parseString = require('xml2js').parseString;
var xpath = require('xpath');
var dom = require('xmldom').DOMParser;
var XLSX = require('xlsx');
var Excel = require("exceljs");
var should = require('chai').should();
// var HashMap = require('hashmap');
var colors = require('colors');
require('/xmlValidator/dbConnect.js');

const PASS = "PASS";
const FAIL = "FAIL";
const INVALID_PATH = "INVALID_PATH";

function printResult(elementMap) {
    var key, result, 
        map = { PASS: 0, FAIL: 0, INVALID_PATH: 0 },
        colorNames = { PASS: 'black', FAIL: 'red', INVALID_PAT 

Post Status

Asked in February 2016
Viewed 3,987 times
Voted 9
Answered 1 times

Search




Leave an answer