Function coloring¶
Category: Async · Status: stub · Lessons: chapter 06, Async (planned)
One line: An async function can call a sync one, but not the other way round without help, so async-ness spreads up the call graph and splits libraries into two colors.
Also called: what color is your function, sync and async functions.
How it connects¶
- See also: Async and await, Blocking the event loop
In each language¶
| Rust | futures are lazy until awaited, and Rust bundles no runtime, so sync code needs one from a crate to run an async function (Book ↗) |
| Go | no colors: any function can run in its own goroutine with a go statement ↗, and a blocking call does not hold up the other goroutines (FAQ ↗) |
| Java | JEP 444 ↗ chose virtual threads over async/await because async/await would split the world between APIs for threads and APIs for coroutines |
| Python | await, async for and async with are allowed only in a coroutine function ↗; sync code starts one with asyncio.run ↗, and async code can hand a blocking call to asyncio.to_thread ↗ |
| JavaScript | await ↗ is allowed only inside async functions and at the top level of a module |
| Kotlin | a suspending function can be called only from another suspending function (basics ↗); runBlocking ↗ bridges from blocking code by blocking the thread |