I have the following loop:
while (true) {
await f();
await g();
}
where f and g are defined as follows:
async function f() {
await Promise.all([SOME_REQUEST_F, sleep(1000)])
}
async function g() {
await Promise.all([SOME_REQUEST_G, sleep(5000)])
}
Also sleep is defined as follows:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
My intention is to have SOME_REQUEST_F awaited every one second, and SOME_REQUEST_G awaited every five seconds, hence wrapping them in f and g.
However, currently g is blocking the re-awaiting of f within the loop.
How to define sleep, such that if used in g, it blocks re-execution of g, but not that of f? Is there a better way to do what I want?
Use two while loops instead:
(async () => {
setTimeout(async () => {
while (true) {
await f();
}
});
while (true) {
await g();
}
})();
The setTimeout there is needed to allow both whiles to run concurrently, so that the initialization of one doesn't block the other from starting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With