auto-reset-event
This package mimic the behavior of AutoResetEvent
of C#, an acquire-release one semantic.
Background
Although JavaScript is single-threaded, one might want to limit number of concurrent asynchronous operations. For example, a task that requires multiple asynchronous steps to complete can use this package to limit its concurrency.
How to use
Run npm install auto-reset-event
.
Code snippets
; // Create an acquire-release queueconst acquire = ; // Acquire a single lock, this function will not be resolved until the lock is acquiredconst release = await ; // Do the non-parallelized asynchronous work // Release the lock;
Full sample
;; { const acquire = ; await Promiseall async { const release = await ; console; await ; ; } async { const release = await ; console; await ; ; } ; console;} ;
Samples
auto-reset-event
can be used to limit concurrency for asynchronous operations.
Slowing down web traffic for debugging purpose
We can use auto-reset-event
to easily slow down concurrent requests by limiting its concurrency and serving rate. In this sample, we slowed down serving static content by serving one request every second.
;;;; const server = ;const acquireSlowLock = ; server; server;
Contributions
Like us? Star us.
Want to make it better? File us an issue.
Don't like something you see? Submit a pull request.