4

I have the following problem: need synchronously wait until a file exists on a certain path.

More details on my case: Files are created by other process. I'm patching node's require which is only sync. No way work it around with async way.

So I can just do:

while(!fs.existsSync(filePath)) {}

or some more sophisticated stuff with a timeout, etc

But I'm not sure if this is a most efficient way to do this: while waiting to perform synchronously fs.existsSync.

Just wanted to hear any other suggestions?

WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181
  • If the file is getting created by the same process, you're going to run into problems because that loop would block. As far as I'm aware, timeouts are only async. I don't know what your code is like or trying to do, but usually waiting for something to happen is a pretty good chance to embrace async. Maybe the better option would be to wait until the file exists to invoke your function? – Brett Beatty Jun 22 '17 at 18:55
  • No the file is going to be created by another process, I believe it woudn't work at all if by the same process. The thing is that I'm patching node's `require` which is only sync. No way work it around with async way. – WHITECOLOR Jun 22 '17 at 19:02

0 Answers0