[HN Gopher] Go-like channels in 10 lines of JavaScript
___________________________________________________________________
Go-like channels in 10 lines of JavaScript
Author : pcattori
Score : 10 points
Date : 2023-02-04 22:20 UTC (39 minutes ago)
(HTM) web link (pedrocattori.dev)
(TXT) w3m dump (pedrocattori.dev)
| lilactown wrote:
| AFAICT after the first write, the inner promise is resolved and
| will return the same value it was resolved with even if
| subsequent writes are made. This is totally different than Go-
| like channels which can receive multiple writes, often handling
| buffering them until they are consumed.
| pcattori wrote:
| That's correct. The last three paragraphs of the article cover
| this and include links to libraries that have "full" channel
| implementations.
| samsquire wrote:
| This is interesting.
|
| Correct me if I don't understand is this only concurrent with
| respect to IO is that right? If you run a IO operation or network
| call (fetch api) in those promises, those shall be concurrent
| with the CPU execution of your Javascript?
|
| I think async/await is a great primitive. I've been working on
| implementing a async/await state machine in Java for
| multithreaded async/await. I want to handle the scheduling of
| multiple promises eagerly, so when you call async task1(); async
| task2(); async task3(); it schedules them all independently on
| different threads.
|
| To do interleaved concurrency in Javascript, you could do the
| same pattern or do my concurrent looping approach. Essentially
| you create a state machine and you switch between tasks with a
| scheduler loop, so you do a bit of work on each task
| independently. It's concurrent but not parallel.
|
| here's my writeup of concurrent loops
| https://github.com/samsquire/ideas4#133-concurrent-loops---l...
|
| here's my java state machine of multithreaded async/await in
| Java, that could be adapted to Javascript except it wouldn't be
| parallel.
|
| https://github.com/samsquire/multiversion-concurrency-contro...
___________________________________________________________________
(page generated 2023-02-04 23:00 UTC)