I'm fairly new with node, and I've been learning and dealing with async/promises. Right now I'm trying to create a process which makes an insert from a DB (10K rows for example), calls a webservice that transforms one column and then make an insert for the modified data.
So, I make an Oracle SQL query, and for the result, I do a foreach:
let counter = 0;
var binds = [];
res.rows.forEach((row) => {
var original_data = row[0];
Transform(original_data).then(new_data => {
counter++;
binds.push([original_data,new_data]);
if (counter % 1000 === 0){
console.log(`1K rows`);
doInserts(binds);
binds = [];
}
});
});
I'm calling the doInserts
every 1000 rows, so I don't have many transactions open on Oracle.
The Transform function calls a webservice, which resolves with the value that I need.
function Transform(value){
return new Promise(function(resolve, reject){
var requestPath = `http://localhost:3000/transform/${value}`;
var req = request.get(requestPath, function(err, response, body){
if (!err && response.statusCode == 200){
resolve(body);
}else{
reject("API didn't respond.");
}
}).end();
});
}
However, this is clogging the webservice (I use the request library to connect) when the foreach has 10K rows. I am thinking, the foreach is not doing the Transform one at a time synchronously.
This is probably me not knowing lots of node, async, promises.. but I'm puzzled. Can anyone help?