by cheez · 7y ago · 0 comments
Here is synchronous code:
result = server.getStuff()
print(result)
Here is synchronous code, that tries to be asynchronous: server.getStuff(lambda result: print(result))
Once server.getStuff returns, the callback passed to it is called with the result.Here is the same code with async/await:
result = await server.getStuff()
print(result)
Internally, the compiler rewrites it to (roughly) the second form. That's called a continuation.That's pretty much it.
A more involved example.
Synchronous code:
result = server.getStuff()
second = server.getMoreStuff(result+1)
print(result)
Synchronous code that tries to be asynchronous: server.getStuff(
lambda result: server.getMoreStuff(
result+1,
lambda result2: print(result2)
))
A lot of JS code used to look like this hideous monstrosity.Async/await version:
result = await server.getStuff()
second = await server.getMoreStuff(result+1)
print(result)
Remember again, that it is basically transformed by the compiler into the second form. result = await server.getStuff()
second = await server.getMoreStuff(result+1)
print(result)
`await getStuff()` MUST terminate before `await getMoreStuff() ` begins. So this chunk alone is analagous to synchronous code, unless we're in the middle of a spawned task, and there are other spawned tasks in the executor that can be picked up.Async/await is really popular in the JavaScript community because in web apps, you usually only have a single thread of execution which you share with the browser UI code. So if your code made a network request synchronously, the user might not be able to scroll or click links or anything until it finished.
Frankly, in the case of sequential flow like the above, I would rather write
result = server.getStuff()
second = server.getMoreStuff(result+1)
print(result)
and have the runtime automatically perform work-stealing for me. No need for awaits. They just litter the code. This is what Go does.Gevent in Python does something similar [implicit switching] using a dirty monkeypatching. It is great while it works. Sooner or later the explicit cooperative concurrency such as provided by async/await syntax wins (e.g., asyncio, trio, curio Python libraries)
Indeed, since you mention continuations, I'm sure you realize that they're more or less callbacks.
server.getStuff(gotStuff)
server.getMoreStuff(gotMoreStuff)
Just using functions is more simple and also more powerful.
A function being async usually means stuff will happen, things can go wrong, and you might want to do different things depending on the response or whether it failed.Where await is useful though is in serial execution of async functions that really should be sync, but them being async is an optimization in order to not block the thread.
It is really unfortunately that so much extra crud had to be introduced to JS (corutines, async, promises) in order to be able to await. Reading complex Promise based code is very unplesent, with async functions pretending to be pure, without any error handling, full of side effects, and omitted returns.
With callbacks we had inexperienced programmers writing pyramids of callbacks and if logic. But it was not that bad, as the complexity was in your face, and not hidden under layers of leaky abstraction.