Following is my code structure. I have two async functions from source.js
and db.js
. I’m exporting these functions and calling them in test.js
.
test.js
calls the function async loadFromFile()
to read and return a list of json data elements. For every element in the list, it then calls async writeToDB()
.
Expected: N number of true
in console
Result: Only 1 true
output in console and only one element written to the database.
// source.js
async function loadFromFile() {
const data = await fs.readFile('/path//to/.json', { encoding: 'utf8' });
const ret = JSON.parse(data);
return ret;
}
exports.loadFromFile = loadFromFile;
// db.js
async function writeToDB(data){
console.log(data.length); // THIS shows N data elements
const query_str = "...";
try{
const response = await driver.executeQuery(query_str, ...);
if (response){
return true;
}
} catch(err){
console.log('Error in query: '+ err);
} finally {
await driver.close();
}
// something went wrong
return false;
}
exports.writeToDB = writeToDB;
// test.js
const source = require("./source")
const db = require("./db")
source.loadFromFile()
.then(data_list => {
console.log(data.length); // This shows N data elements
for (json_data of data_list){
db.writeToDB(json_data)
.then(resp => console.log(resp))
.catch(error => console.error(error));
}
})
.catch(error => console.error(error));
2
Answers
Thanks to @"Daniel A white", @Bergi and @"Bilal Azam". Removing
driver.close
and calling the functions in their separateasync-await
solved the issue. The final code looks as followsLog output is as expected and all the data gets written to the DB as expected. However, the running
node test.js
on terminal gets stuck and never exits unless done manually usingctrl-c
.You are currently not awaiting the
writeToDB
calls inside the loop, and so time taken inside this loop isn’t going to get extended for the completion of these asyncops to finish execution.Here is your test.js code corrected, waiting on each writeToDB call properly: