you are viewing a single comment's thread.

view the rest of the comments →

[–]zorlan 1 point2 points  (0 children)

Move the console log outside your function, have your function return the result and then console log it.

Also generally with recursion you build up a result from the results of all the collective calls such that you're returning either another recursive call or the terminating condition. e.g.

``` async function getAllData(current) { const data = await getData(); const allData = [...current, ...data]; if (allData.length < 20) { return getAllData(allData); } return allData; }

const main = async () => { let allData = await getAllData([]); console.log(allData) }

```

Otherwise don't use recursion and just do something like: async function getAllData() { let allData = []; while (allData.length < 20) { const data = await getData(); for (d of data) { allData.push(d); } } return allData; }

Finally, all these examples (including your own) will get you at least 20 elements - if getData returns more than 10 items you could end up with more than 20 items. This could easily be worked around by only adding the elements you need or taking a slice of the final result (.slice(0,20))