1

I'm writing a node.js program that makes use of the fibrous library to maintain synchronous control in part of my code.

In a helper function, I need to complete a timeout, but cannot use setTimeout() because it will not work in the context with fibrous.

This is what I came up with instead:

startTimer = new Date()
startSecondTimer = new Date()
while startSecondTimer - startTimer < 10000
    startSecondTimer = new Date()

I'm not very comfortable with creating several thousand Date objects to support this workflow, but I'm not certain what a better approach here might be.

Is there a more optimal way to write a synchronous timeout-style function?

Scimonster
  • 32,893
  • 9
  • 77
  • 89
fox
  • 15,428
  • 20
  • 55
  • 85
  • It looks like you could easily use `setTimeout` [inside a `wait`](https://github.com/goodeggs/fibrous#4-waiting-on-a-callback), or just go with [Fiber's `sleep` implementation](https://github.com/laverdet/node-fibers#sleep)? – Bergi Nov 16 '14 at 23:45

1 Answers1

1

There is library for this node-sleep in C++ (fallback on Windows like your while loop). Node is not meant for such a things even more, have you notice what is this operation doing with CPU.

As a hint How to create a sleep/delay in nodejs that is Blocking?.

Community
  • 1
  • 1
kwarunek
  • 12,141
  • 4
  • 43
  • 48