I need to pull data from an API, but it sends me the data in pages of 100 + the total count of results. The way I tried doing is
let count, page = 0;
do {
++page
pullData(page, () => {
count = data.count
});
} while (page < count / 100);
but the code checks the condition of the loop before count is initialized (page < undefined / 100), and the loop ends up running only the first time.
Is there a way to make the loop wait for the async body to run before checking the condition?
await.await count = data.countandawait pullDatawith no successpullDatareturns a promise that should work, but if it expects a callback function then you need to "promisify" it first.awaitto work,pullDatamust return a Promise, andawaitkeyword must be used inside function withasynckeyword beforefunction, likeasync function Foo () ....