Funnel a function's invocations into a queue or guard them with a nonblocking mutex.
Forces a function's calls into a queue, causing them to execute sequentially.
Example:
let serialFetch = queued(function(release, a) {
api.fetch(a, function(result) {
process(result); // Do something with result
release(); // Release exclusive lock
}, release); // We provide `release` as an error callback to `api.fetch`
// so that the lock will be released even if the find
// fails
});
You can impose a timeout on exclusivity by adding { timeout: <milliseconds> }
as an argument to queued()
.
Example:
let serialFn = queued(function(release) {
doLengthyWork();
release();
}, { timeout: 100 });
Here, the release
callback will be automatically called after 100 milliseconds,
allowing the next call to be executed even if the current one is still in progress.
Wraps the provided function in a nonblocking mutex, preventing
reentry into the function until the provided release
callback is called.
While an execution of the function is in progress, all would-be concurrent calls are thrown out.
passThrough
accepts a timeout as well via { timeout: <milliseconds> }
.
- switch to Promises and return function's results / error
- support custom release/timeout functions
- optionally raise an exception when lock cannot be obtained