jochemstoel / nodejs-system-sleep

Sleep function for Node.js All platforms.
https://www.npmjs.com/package/system-sleep
The Unlicense
51 stars 8 forks source link

sleep(0) should not stop execution #19

Closed obrassard closed 4 years ago

obrassard commented 4 years ago

We're using system-sleep in a project and I noticed that sleep(0) stop the execution of the code for a variable amount of time. However, since the delay is 0ms, I think this shouldn't happen.

Edit : In fact, it looks like sleep(0) is just completely stopping the execution of the code..

jochemstoel commented 4 years ago

@obrassard What operating system are you using? I was not able to reproduce this on Windows.

// sleep.js
const sleep = require('system-sleep')
console.log('moo')
sleep(0)
console.log('foo')
> node sleep.js
moo
foo

Exits successfully after 0ms. Can you provide more details, like your environment, code or any error message? Then I can look into it.

Edit: I would like to add that in most cases, a sleep(0) is redundant and should be avoided.

obrassard commented 4 years ago

Hello @jochemstoel ! My apologies, I should have given you more information about my environment. I'm currently running MacOS Catalina and I'm using node v14.5.0. Indeed, it seems that my colleague (who uses Windows) doesn't have this problem either. There is no particular error message, the execution just freezes.

I would like to add that in most cases, a sleep(0) is redundant and should be avoided.

I agree with you. In our case, we use sleep() to simulate network latency on an API, and 0 was passed to the sleep function when someone makes a request without "fake latency". That's how I discovered the issue. Of course, we can easily deal with this by wrapping "sleep(delay)" in an if statement, but I wanted to report the problem here nonetheless.

jochemstoel commented 4 years ago

@obrassard I have added a type check for the interval argument and a value check for when the value of interval is 0. This will hopefully solve your problem as no additional code is being executed when there is no positive interval value. If it doesn't fix it then it is probably caused by something else on your MacOS system. To be sure of this, try it on a different MacOS system.

You can npm install system-sleep for the latest version 1.3.7 or get it here from the updated Github repository.

Please do let me know if this worked.

obrassard commented 4 years ago

Great, I'll test it this afternoon I'll let you know.

obrassard commented 4 years ago

Hi @jochemstoel, I can confirm the v1.3.7 fix the issue on my side ! Thanks 👍