Earlier quoted context omitted.
No. All the JS code is still executing serially in the same event queue. This is a good thing, lean into the guarantees it provides.
In an async function, the only guarantee you have is that code between two await points is not interruptible. As you have more and more async operations, this guarantee gets pretty weak (if you have more than one pending async task and the current task yields, which one will run next?) and you have to start employing the usual locking mechanisms to ensure correctness.
Go-like channels in 10 lines of JavaScript
61–65 of 65 posts
Re: Go-like channels in 10 lines of JavaScript
#62Perhaps a sign that it’s finally over, and typescript has won in the community.
Re: Go-like channels in 10 lines of JavaScript
#63Author refers to typescript code as “javascript,” considering the distinction no linger meaningful. Perhaps a sign that it’s finally over, and typescript has won in the community.
But actually:
> Ignoring the Typescript type definitions, its only 10 lines of Javascript!
So removing the types (JavaScript), it is 10 lines, but if you wanna write it in TypeScript it's 21 lines. Proof how verbose TypeScript can make codebases/examples?
Re: Go-like channels in 10 lines of JavaScript
#64The real essence of Go channels is their ability to participate in the built-in select keyword. Of course, Go does not have access to any magic CPU instructions that make it something Go can uniquely do. But anything that wants to be "Go channels but in X" need to be implementing select, not a send operation. Send operations are easy. And a useful primitive! Way back in the day, Queue in Python was the way to communi…
Disclaimer: I am the author of this library [1]. A few days ago, I ported ocaml/Event [2] to JavaScript, which provides concurrent ML-style synchronization operations. It is possible to implement `Channel` and `select` in JS, but it is not easy to provide an idiomatic API and integrate it with the Promise ecosystem. [1]: https://github.com/dhcmrlchtdj/sync-op [2]: https://ocaml.org/api/Event.html
Ultimately though, I don't believe that channels are an abstraction that makes sense in JavaScript's concurrency model. Go's contexts, on the other hand, would be a huge improvement over AbortController and AbortSignal.
Re: Go-like channels in 10 lines of JavaScript
#65Author refers to typescript code as “javascript,” considering the distinction no linger meaningful. Perhaps a sign that it’s finally over, and typescript has won in the community.
I do see your point though. Typescript has taken off in the last few years.