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));
3
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:
const source = require("./source")
const db = require("./db")
async function processFile() {
try {
const data_list = await source.loadFromFile();
console.log(data_list.length);
for (const json_data of data_list) {
try {
const resp = await db.writeToDB(json_data);
console.log(resp);
} catch (error) {
console.error('Error writing to DB:', error);
}
}
} catch (error) {
console.error('Error loading file:', error);
}
}
processFile();
Thanks to @”Daniel A white”, @Bergi and @”Bilal Azam”. Removing driver.close
and calling the functions in their separate async-await
solved the issue. The final code looks as follows
// 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);
}
return false;
}
exports.writeToDB = writeToDB;
// test.js
const source = require("./source");
const db = require("./db");
async function process() {
const data = await source.loadFromFile();
for (json_data of data){
try {
const resp = await db.writeToDB(json_data);
if (!resp) {console.log('Error writing to DB');}
} catch(err){console.log('Error:'+ err);};
}
}
process()
.then(d => console.log(d))
.catch(error => console.error(error));
Log 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 using ctrl-c
.