Goal: Delay execution of function by at least 1 second.
The problem: Instead of a minimum 1 second delay the do / while
loops forever.
The steps:
- After 1 second
timer
is set totimerLength
. - If the
fetch
request takes more than 1 second no further delay is added. - If the
fetch
request takes less than 1 second further execution will be delayed by up to 1 second bydo / while
loop.
let timer = 0;
const timerLength = 1000;
setTimeout( () => {
timer = timerLength;
}, timerLength);
// fetch request and other code here...
do { } while(timer !== timerLength); // no code inside do while
// Code after this point is expected to run after a minimum delay of 1 second after setTimeout.
2
Answers
If you need to wait for two or more operations to complete before continuing, you need to use promises.
In your case, create a
promise
for your timeout, and use thepromise
returned byfetch
. Pass these to promises to aPromise.all()
so thatPromise.all()
only resolves when both your original promises resolve.E.g.
The steps you outlined will not run in that order because
setTimeout()
(andfetch()
although you removed that using comment syntax) is asynchronous.Asynchronous functions are not run on the Call Stack but are removed and handled differently. Their results are placed on a Task Queue leaving the Call Stack free to run synchronous code.
The Event Loop keeps checking from time to time for a perfect time to take the first callback from the Task Queue and execute it but since you kept the Event Loop busy with an infinite loop, it will never be free to do these other things.
Use an
async
function to run asynchronous code andawait
their result.Take a deep dive into the Javascript Event Loop [MDN] and how to write asynchronous code [MDN].