Go-like channels in 10 lines of JavaScript
pedrocattori.dev
Go-like channels in 10 lines of JavaScript
1–10 of 65 posts
Re: Go-like channels in 10 lines of JavaScript
#2Re: Go-like channels in 10 lines of JavaScript
#3Correct 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 switch statement based 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.
* if you've used protothreads in C, this is what it is similar to.
To do interleaved concurrency in Javascript, you could do the same pattern (switch based state machine) or do my concurrent looping approach. You break up your long CPU task into lots of microtasks and switch between them concurrently with a switch statement. 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 which takes arbitrarily nested loops and turns them into an iterator 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...
Re: Go-like channels in 10 lines of JavaScript
#4AFAICT 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.
Re: Go-like channels in 10 lines of JavaScript
#5This 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 switch statement based state machine* in Java for multithreaded async/await.…
Re: Go-like channels in 10 lines of JavaScript
#6Re: Go-like channels in 10 lines of JavaScript
#7Re: Go-like channels in 10 lines of JavaScript
#8I don’t understand why there is a bang operator in the last code sample on line 18. Is it a typo?
Re: Go-like channels in 10 lines of JavaScript
#9This 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 switch statement based state machine* in Java for multithreaded async/await.…