4 ms·
Nope, it still holds. It’s in fact impossible to call an async function from a sync function and return the result. To use await you have to make the function a
by guntars 7y ago
Nope, it still holds. It’s in fact impossible to call an async function from a sync function and return the result. To use await you have to make the function async which means the caller needs to be async-aware and so on, all the way to the top of the stack.
There are hacks like “deasync”, but I personally wouldn’t use it.
https://github.com/abbr/deasync https://github.com/abbr/deasync
Rust can block on an individual future so, say, a sync callback can still take advantage of async functions.
- qaq 7y agoGiven history of JS not being able to call an async function from sync function is a none issue. JS went from callbacks to promises to async/await (sugar on top of promises).
- littlestymaar 7y agoBut you don't need `await` to call an async function, you can use a regular function call in a symc and then the function returns (synchronously) a Promise. What cannot be done is to perform a blocking call on a Promise from a sync function. And that is by design because JavaScript has a single threaded runtime.