Introducing Async Channels

What?

Channels are queue-like objects (First In First Out) that their enqueue (send) and dequeue (get) functions are asynchronous (async). By passing them between asynchronous functions we can synchronize operations between said functions.


This content originally appeared on DEV Community and was authored by Eyal Shalev

What?

Channels are queue-like objects (First In First Out) that their enqueue (send) and dequeue (get) functions are asynchronous (async). By passing them between asynchronous functions we can synchronize operations between said functions.

Why?

Let's say we want to process a large chunk of data (simulated by a several lorem-ipsum paragraphs).
Some of the processes that we want to perform on that data are fast, while other are time (and resource) consuming.
To avoid overwhelming our infrastructure, we want to make not to send data to a process that isn't ready to accept new data. In other words, we want to implement back-preasure.
This is where Async Channels come into play.

When you send (or get) a message to (or from) a channel, it returns a promise.
For send requests, it will resolve if the channel has available buffer, or (if not), after a get is performed on the channel.
And on the opposite side, a get request will resolve if there is buffered message on the channel, or after a send request is made.

How?

Below is an example usage of async_channels, where long processes are simulated by calling sleep (a helper function that returns a promise that is resolved after n seconds).



You can view the documentation on eyal-shalev.github.io/async_channels

GitHub logo Eyal-Shalev / async_channels

Inspired by Go & Clojure Channels, async_channels provides channels as an asynchronous communication method between asynchronous functions.


This content originally appeared on DEV Community and was authored by Eyal Shalev


Print Share Comment Cite Upload Translate Updates
APA

Eyal Shalev | Sciencx (2021-10-09T13:44:04+00:00) Introducing Async Channels. Retrieved from https://www.scien.cx/2021/10/09/introducing-async-channels/

MLA
" » Introducing Async Channels." Eyal Shalev | Sciencx - Saturday October 9, 2021, https://www.scien.cx/2021/10/09/introducing-async-channels/
HARVARD
Eyal Shalev | Sciencx Saturday October 9, 2021 » Introducing Async Channels., viewed ,<https://www.scien.cx/2021/10/09/introducing-async-channels/>
VANCOUVER
Eyal Shalev | Sciencx - » Introducing Async Channels. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/09/introducing-async-channels/
CHICAGO
" » Introducing Async Channels." Eyal Shalev | Sciencx - Accessed . https://www.scien.cx/2021/10/09/introducing-async-channels/
IEEE
" » Introducing Async Channels." Eyal Shalev | Sciencx [Online]. Available: https://www.scien.cx/2021/10/09/introducing-async-channels/. [Accessed: ]
rf:citation
» Introducing Async Channels | Eyal Shalev | Sciencx | https://www.scien.cx/2021/10/09/introducing-async-channels/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.