I have a problem, which I try to solve but I'm a bit lost but I think it's simple and you could help me.
I have a function that obtains data, each data can take time to receive it, or even nothing is received, that's why I include several loops in the function that allow me to retry a maximum of attempts and get data of a max number of elements.
// get from zero to max number of elements
export const getFromZero = async (uri: string, max: number) => {
log(`Fetching ${chalk.white.bold(max.toString())} tokens`, "info");
let data = [];
let i = 0;
while (i <= max) {
// Find and Retry
let re = await get(uri, i, max);
if (re === null) {
for (let j = 0; j < 20; j++) {
re = await get(uri, i, max);
if (re !== null) {
break;
}
}
if (re === null) {
log(`Element ${chalk.white.bold(i.toString())} not found`, "error");
}
}
if (re) {
data.push(re);
}
i++;
}
log(
`Obtained ${chalk.white.bold(data.length.toString())} elements`,
"success"
);
return data;
};
The problem here is that I search for the data 1 by one using the await function before moving on to the next search, which causes the process to be slow by having to receive, for example, 5000 elements, what I want to do is that the searches be done all at the same time, or wait a minimum amount of MS between each request, but I want to return the data at the end of the function once all the requests have been received or the retries of each one have been exhausted, I need to obtain all the data without having to wait between 1 call and another but only return it at the end of the process.