4 ms·
I might be showing my inexperience here, but my understanding of async/await and promises in JS is that they create threads under the hood to solve the problem
by vermilingua 5y ago
I might be showing my inexperience here, but my understanding of async/await and promises in JS is that they create threads under the hood to solve the problem the author is talking about.
I'm afraid I just don't quite understand why async/await don't solve the problem and instead sweep it under the rug, could someone explain it?
- dkdbejwi383 5y agoIf you want to use ‘await’, the calling scope needs to be an async function, which means any other function which calls the now async caller also needs to be async (well, if you need the resolved value. You can just orphan promises but then you’ll probably have unintended consequences). You’re forced to propagate it up the chain, which one may argue is a leaky abstraction.
- yuchi 5y agoAbsolutely not. JS is single thread and async/await is more or less a syntax sugar around promise APIs.
- brazzy 5y ago> my understanding of async/await and promises in JS is that they create threads under the hood to solve the problem the author is talking about. No, that is wrong in multiple ways. Javascript is fundamentally designed around an event loop processed by a single thread. The only multitreading facility it has are web workers, which don't share memory with the main thread. > I'm afraid I just don't quite understand why async/await don't solve the problem and instead sweep it under the rug, could someone explain it? Both are really only syntactic sugar over callbacks. Under the hood, async/await splits the await-ing function and passes the second part as a callback to the await-ed function. And it requires the await-ing function itself to be async. Promises are just a nicer way to do callbacks. It's impossible to get a result from a promise (or an ansync function) synchronously. If there were a way, it would block the main thread and kill performance completely.