Why is do / while looping infinitely after condition was expected to have been met?

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 to timerLength.
  • 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 by do / 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.