Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Built-in low-cost async to sync method #34918

Closed
septs opened this issue Aug 25, 2020 · 8 comments
Closed

Built-in low-cost async to sync method #34918

septs opened this issue Aug 25, 2020 · 8 comments
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs. feature request Issues that request new features to be added to Node.js.

Comments

@septs
Copy link

septs commented Aug 25, 2020

reduce unstable conditions caused by such hacking
https://npm.im/deasync

import { deasync, depromise } from "util";

const sleepWithSync = deasync((timeout, done) => { setTimeout(done, timeout) });
const promiseWithSync = depromise(async (input) => input)

sleepWithSync(1000)
promiseWithSync(1) == 1
// sync done
@himself65 himself65 added the feature request Issues that request new features to be added to Node.js. label Aug 25, 2020
@bnoordhuis
Copy link
Member

reduce unstable conditions caused by such hacking

Unclear description. What are you asking or saying?

@devsnek
Copy link
Member

devsnek commented Aug 25, 2020

Looks like a duplicate of #30634

@oussematn
Copy link

please review your question

@jasnell
Copy link
Member

jasnell commented Aug 25, 2020

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.

@sindresorhus
Copy link

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?

@sindresorhus
Copy link

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

@devsnek
Copy link
Member

devsnek commented Aug 25, 2020

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.

@devsnek
Copy link
Member

devsnek commented Aug 25, 2020

Since this is a dupe, closing out.

@devsnek devsnek closed this as completed Aug 25, 2020
@devsnek devsnek added the duplicate Issues and PRs that are duplicates of other issues or PRs. label Aug 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs. feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

7 participants