Why is looping with Array.concat giving me this result on Hackerrank?

  Kiến thức lập trình

I took a Hackerrank screening and ran into this issue with using Array.concat(). I was wondering what I’m missing?
The function was essentially:

async function getData(id) {
let data = []
let end = false
while (!end) {
    const result = await getSomeResult(id)
    data = data.concat(result['data'])
    if (result.page == result.total_pages) {
        end = true
    }
}

return data
}

The total length of data in the test case is supposed to be 67. When I ran the test case, the length was 7 – the page size is 10 so I’m thinking the final loop’s data = data.concat(result['data']) is setting data to [].concat(~finalResultData~). When I added a ws.write to the loop, data was correct (67) before the return, but weirdly enough, when we go back to the calling function, the result length was again 7! Am I missing something with async await here?

LEAVE A COMMENT