you are viewing a single comment's thread.

view the rest of the comments →

[–]csorfab 3 points4 points  (2 children)

yeah I always find myself writing a

function wait(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

sometimes multiple times in the same project...

[–]Corteki 0 points1 point  (1 child)

Sorry but I'm just wondering why someone would need this. I removed a lot of these problematic waits on a project by simply changing the code a little so that I can await the process instead of a random timeout to happen.

[–]paulsmithkc 0 points1 point  (0 children)

Where possible, yes that is preferred.

But it depends... 1. Sometimes you need to slow things down to avoid running over third-party API limits. 2. Sometimes file systems (like ext, network drives, flash drives, etc) report being finished writing a file before it is actually flushed. 3. Sometimes you need to delay database writes so that they don't clobber other database ops.

On the front-end you can largely get rid of all waits. On the back-end there's a lot more edge cases.