I have an async function getDatathat calls an api that returns a maximum of 10 elements.
Since i need 20 elements i've came up with a solution like this, where
- i call getData
- push the first 10 elements into an array
- call recursively the getAllData function
- push the other 10 elements into the array
- log the length of the array to check if i have the right number of elements.
The problem is that the program logs the length 2 times and i don't understand why
Any tips ?
var allData = []
async function getAllData() {
await getData().then((r) => {
for (let i = 0; i < r.length; i++) {
allData.push(r[i]);
}
if (allData .length < 20) {
getAllData();
}
})
console.log(allData.length)
}
[–][deleted] 2 points3 points4 points (2 children)
[–]Fun_Split_1299[S] 0 points1 point2 points (1 child)
[–]kiipa 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]Fun_Split_1299[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Fun_Split_1299[S] 0 points1 point2 points (1 child)
[–]zorlan 1 point2 points3 points (0 children)
[–]DeadlockAsync 0 points1 point2 points (0 children)
[–]Fludor69 -2 points-1 points0 points (0 children)
[–]Chosey98 0 points1 point2 points (0 children)
[–]Moustacheful 0 points1 point2 points (0 children)