abbr / deasync

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

Getting infinite while loop using Promise, then #151

Open aryanrajput opened 3 years ago

aryanrajput commented 3 years ago

var result = null; var isDone = false;

const p = new Promise((resolve, reject) => { setTimeout(function(){ console.log("This is set time out") resolve('resolved') }, 4000) })

p.then(res =>{ result = res; isDone = true; console.log("This is then section.") })

while(!isDone) { console.log("in while", result) deasync.sleep(10000); }

console.log(data)

anonghuser commented 1 year ago

no need for any timers to get a reproduction, even just this simple code hangs:

import deasync from 'deasync'
const test = deasync(function(r, cb) {
    Promise.resolve().then(()=>cb(null, r))
})
console.log(1)
console.log(test(2))
console.log(3)