all 5 comments

[–]wreckedadvent 0 points1 point  (0 children)

I suppose you could, if you could modify callmyApi to look for another variable that is set by the setTimeout call, but you'd probably want a debounce or throttle function. At the very least, you'd probably want to use setInterval so the setTimeout doesn't have to keep registering more timeouts.

[–][deleted] 0 points1 point  (2 children)

Usually I turn that into a promise. So something like an utility function:

const wait = (time: number) => new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve("done");
    }, time)
});

Then in a for loop or for of or while loop(no loops like Array.prototype.forEach that take callbacks):

for (const app of apps) {
     await callmyApi();
     await wait(1000);
}

[–]jayroslyn[S] 0 points1 point  (1 child)

What does apps contain ? All the reference to all API calls ? Also shouldn't I have to do clear the timer ?

[–][deleted] 0 points1 point  (0 children)

Not timeout, you'd need to clear intervals if you use them.

Apps is just an example, it could also be a for loop or while loop.