Closed septs closed 4 years ago
reduce unstable conditions caused by such hacking
Unclear description. What are you asking or saying?
Looks like a duplicate of #30634
please review your question
Given the way that Promises and async operations work within JavaScript and Node.js, converting those into sync blocking operations is not actually possible. The blocking to wait for completion would block the actual completion because the task queues would never be drained and the event loop would never turn to allow the Async ops to complete. There are ways you can come close to what you want using Worker threads but at a fairly steep performance and complexity price.
If you believe there's a way to accomplish it in a reasonable way, pull requests are always welcome.
Given the way that Promises and async operations work within JavaScript and Node.js, converting those into sync blocking operations is not actually possible.
How is desync
able to do it then?
Sidenote: If you just need sleep synchronously for a certain amount of time, this can now be achieved in pure JS: https://github.com/sindresorhus/sleep-synchronously/blob/1fe452b47e810cf21bbc67e022a70ab925603759/index.js#L4
From the dupe issue:
I should've been a bit clearer. It's not possible to take an arbitrary function that performs some unknown async behaviour and make it synchronous. deasync exploits the fact that the majority of async behaviour in node applications is very similar: one-shot calls to libuv apis. However, as with the bug you linked, not everything follows that pattern.
Node is able to provide sync apis because it has specialized C++ code for those individual synchronous apis. We don't have an internal magic "syncify" api that could be exposed.
Since this is a dupe, closing out.
reduce unstable conditions caused by such hacking https://npm.im/deasync