I'm trying to populate my list
with some data return from the function function
but this approach didn't work.
function.findSomeData
request data from oracle database and my index
is reaching list.lenght before callback from function.findSomeData
for (index; index < originalList.length; index++) {
this.function.findSomeData(
param1,
null,
null,
param2,
conn,
true,
lang,
(err, result) => {
if (err) {
super.doReleaseNewConn(connection, conn, !autoCommit);
callback(err, null);
} else {
if (!Validator.isNullUndefinedEmpty(result)) {
list.push(result[0]);
}
if (index >= originalList.length) {
callback(null, list);
}
}
}
);
}
Which is the best and correct way to do this?
-- Solved by updating findSomeData
:
Now, i'm calling findSomeData
passing an index and both lists (originalList, finalList). Inside de function, i'm interating the db request controlling the index
by originalList.lenght and pushing the returned data into finalList, as:
this.function.findSomeData(
0,
originalList,
list,
param2,
conn,
true,
lang,
(err, result) => {
if (err) {
super.doReleaseNewConn(connection, conn, !autoCommit);
callback(err, null);
} else {
if (!Validator.isNullUndefinedEmpty(result)) {
list.push(result[0]);
}
if (index >= originalList.length) {
callback(null, list);
}
}
}
);
And
public findSomeData(
index: number,
originalList: [],
list: [],
connection: Connection,
autoCommit: boolean,
lang: string,
callback: (errorBuscaListaCliente: string, list: []) => void
) {
try {
if (indice < listaUc.length) {
--db requests and another things
} else {
callback(null, list);
}