Live data from Hacker News

Go-like channels in 10 lines of JavaScript

pedrocattori.dev

61–65 of 65 posts

Re: Go-like channels in 10 lines of JavaScript

#61
post #45

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.

From experience, it is a strong enough guarantee to prevent many race conditions.

Re: Go-like channels in 10 lines of JavaScript

#63

Author 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 think the author might just have a problem labeling things properly. Not only the JavaScript/TypeScript confusion, but the code snippet is also 21 lines, not 10 lines.

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

#64
post #46
post #23

The 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

I took a stab at this a while back using an object to represent the possible resolutions. The keys of the object become a signal upon resolution that indicates which branch fired. https://github.com/ggoodman/channels#select-key-string-chann...

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

#65

Author 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 mean, in this case there's not really a difference with regards to the actual substance of the code.

I do see your point though. Typescript has taken off in the last few years.

Post reply on HN