My use case: a Markdown parser that should work both synchronously and asynchronously. The latter forces the invoking code to switch to async, which is overkill when parsing simple strings.
Currently the parser is based on async generators.
I did some experiments with CSP.
Approach:
- Replace async generators with sync generators.
- Use
yield
instead ofawait
. - Use a queue for output instead of
yield
.
The generators are controlled by a driver. There are two versions of the driver/queue combo:
- Async version: The queue is based on Promises. The driver awaits when there is a
yield
. - Sync version: The queue returns values (directly or inside lightweight wrappers). The driver mostly just passes the values on.
Alas, I was never completely happy with the results of my experiments. The code always felt off.
- Original code is based on async iteration and asynchronous helpers (functions and/or objects).
- A synchronous version is produced by removing every “async” from the core code and by replacing the async helpers with synchronous ones.