I want to run the following code to get the result of 1,2 (a pause of 2 seconds)3,4,5. But because of the non-blocking nature of Javascript I will get 1,2,4,5 (pause of roughly 2 seconds) 3.
I have the following code which includes some asynchronous code which will cause the 3 to show at the end since the console.log(3) won't be called until the timeout completes and the remaining code runs since it is not blocked.
console.log(1);
console.log(2);
setTimeout(()=> { console.log(3); }, 2000);
console.log(4);
console.log(5);
My current code is definitely not optimized for Javascript (or will even work as intended without some changes) but sometimes blocking code vs non-blocking code is easier to write and still performs adequately even if it is less efficient.
Is there any sort of way other than chaining callbacks, async/awaits etc... to kind of force Javascript into a less efficient blocking mode where all lines of code (Async or Sync) will be forced to wait and run one at a time in the order they are experienced?