3 ms·
const x = somethingAsync(); const y = somethingAsyncToo(); return { foo: await x, bar: await y } There is no point in returning one before the other beca
by RedShift1 5y ago
const x = somethingAsync();
const y = somethingAsyncToo();
return { foo: await x, bar: await y }
There is no point in returning one before the other because you need both?
- aGHz 5y agoBut in JavaScript, these two awaits will not happen in parallel, you really need to await Promise.all() for that.
- tehbeard 5y agoYou've really missed the point spectacularly of that example. Both those promises start, and both are waited for after both have started.. That is the same as promise.all... There's just an explicit order for the wait, rather than as they resolve, but the result is the same. Now, promise.any.... You'd have a point...
- aGHz 5y agoThis is correct, I wasn't paying attention.
- deleted 5y ago[deleted]
- linkdd 5y agoIn Javascript, a Promise is started as soon as it is created. In other words, this is not the `await` that starts the Promise. If the first await is the slowest, the second one will return immediately (like calling .then on an already resolved promise).
- matt-attack 5y agoPretty sure there can be unexpected behavior if you wait too long before you do those awaits at the end.
- RedShift1 5y agoNo, why would there be?
- matt-attack 5y agoNode 16 will exit if an exception is thrown and not awaited for some finite period of time. So if your goal is to keep those promises in some cache and then resolve them later on at your leisure, you will find the entire node process will abort. There is a feature flag to restore the older behavior but it’s a pretty big gotcha.
- RedShift1 5y agoThere is no such finite period of time. You can call an async function and never await it. Exception handling is something completely different. Yes, if you call an async function and do not catch the exception, Node will stop. But that is independent of having called await or not. Whether or not you await something async does not affect exception behavior.
- codethief 5y ago+1 Notably, this is different in Python where promises (futures) are executed lazily, i.e. when you await them.
- saimiam 5y agoI think you’re trying to recreate the semantics of Promise.all without using Promise.all. You’re effectively saying that Promises are a better async programming paradigm than async/await…which is also what the author is saying in the article.
- RedShift1 5y agoI'm not saying anything about promises vs async/await. The original comment said that you can't have 2 async things happen in parallel without Promise.all, my code snippet proves that you can.