Hacker News new | past | comments | ask | show | jobs | submit login

I don't think this will work in JavaScript because that while loop will block the event loop and prevents callback from executing. For example, the following code will be blocked when running in Node.js:

    function delayedValue(val, ms) {
        return new Promise(resolve => setTimeout(() => resolve(val), ms))
    }
    
    function syncFn() {
        let result
        let state = 'pending'
    
        delayedValue(1234, 500)
            .then(data => {
                result = data
                state = 'fulfilled'
            })
            .catch(error => {
                result = error
                state = 'rejected'
            })
        while (state == 'pending') {}
        return result
    }
    console.log('before')
    console.log(syncFn())
    console.log('after')



Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: