nodejs-system-sleep icon indicating copy to clipboard operation
nodejs-system-sleep copied to clipboard

not waking up after sleep

Open aqieez opened this issue 5 years ago • 1 comments

in a while loop, it's not waking up after 2nd time

const express = require('express')
const sleep = require('system-sleep')
const app = express()
app.listen(3000, () => {

while(true){

console.log("in a while loop")
sleep(5000)

}

})

aqieez avatar Feb 18 '20 14:02 aqieez

I am not exactly sure why this happens. It has something to do with Express. Running it on my machine, the console.log call runs 5 times. If you put it outside the callback function of app.listen, it works fine.

app.listen(3000, () => {
   //
})

while (true) {
    console.log("in a while loop")
    sleep(5000)
}

When I put it in a separate function like this:

const express = require('express')
const sleep = require('system-sleep')
const app = express()
const sleeper = () => {
    while (true) {
        console.log("in a while loop")
        sleep(5000)

    }
}
app.listen(3000, () => {
    sleeper()
})

it indeed stops after 2 times. I don't understand why and any comments are very welcome.

jochemstoel avatar Feb 21 '20 19:02 jochemstoel