Hacker News new | ask | show | jobs
by rjmill 212 days ago
> But isn't it true for JavaScript too?

I don't think so. It's been a while since I've bled on tricky async problems in either language, but I'm pretty sure in JS it would be

  [...]
  parent_before
  parent_after
  child_before
  [...]
In JS, there are microtasks and macrotasks. setTimeout creates macrotasks. `.then` (and therefore `await`) creates microtasks.

Microtasks get executed BEFORE macrotasks, but they still get executed AFTER the current call stack is completed.

From OP (and better illustrated by GP's example) Python's surprise is that it's just putting the awaited coroutine into the current call stack. So `await` doesn't guarantee anything is going into a task queue (micro or macro) in python.

2 comments

>I'm pretty sure in JS it would be [...]

That doesn't make sense. That would mean the awaiting function doesn't have access to the result of the Promise (since it can proceed before the Promise is fulfilled), which would break the entire point of promises.

> Microtasks get executed BEFORE macrotasks

Correct.

> they still get executed AFTER the current call stack is completed.

Correct.

> I'm pretty sure in JS it would be [...]

Your understanding of JS event loop is correct but you reached the wrong conclusion.