abbr / deasync

Turns async function into sync via JavaScript wrapper of Node event loop
MIT License
971 stars 73 forks source link

Cannot use sleep concurrently #154

Open MayamaTakeshi opened 3 years ago

MayamaTakeshi commented 3 years ago

Hi, based on code from #62 I tried to make sleep to run concurrently like this:

var deasync = require('deasync')

setTimeout(function(){
    deasync.sleep(250)
    console.log("deasync works! (1)");
}, 1)

setTimeout(function(){
    deasync.sleep(250)
    console.log("deasync works! (2)");
}, 1)

but the code hangs. Is this expected?

ohadcn commented 2 years ago

I have similar problem. have a look at this code:

`const sleep = require('deasync').sleep;

setInterval(e => { console.log("1", new Date()); }, 1000); setInterval(e => { sleep(2000); console.log("4", new Date()); }, 4000); `

the code runs and prints:

1 2022-02-17T13:51:31.060Z
1 2022-02-17T13:51:32.062Z
1 2022-02-17T13:51:33.063Z
1 2022-02-17T13:51:36.064Z <-- here the 1 second interval skipped 2 runs
4 2022-02-17T13:51:36.064Z
1 2022-02-17T13:51:37.065Z
1 2022-02-17T13:51:40.062Z <-- skipped again
4 2022-02-17T13:51:40.062Z