Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
async-guard
Advanced tools
npm install --save async-guard
How often have you worried that an asynchronous operation like this would be called way too many times or even just twice in quick succession, before a service was able to respond with the first answer?
exports.createUser = function (userName, password, cb) {
var postData = {
userName: userName,
password: password
};
httpsPost('http://example.com/some/service/create', postData, cb);
};
Perhaps not enough. Consider the possibility that someone hammers your server with a few too many clicks on the "login", "create user" or other button. Did you just create a user once, then tried again and got an error because the username is taken? Do we now have two users? Very often race conditions like this can cause a degraded user experience.
With async-guard, you can protect yourself from these situations. Let's turn the example above into a safer version:
var callbacks = require('async-guard')();
exports.createUser = function (userName, password, cb) {
if (!callbacks.add(userName, cb)) {
return;
}
var postData = {
userName: userName,
password: password
};
httpsPost('http://example.com/some/service/create', postData, function (error, result) {
callbacks.run(userName, [error, result], console.error);
});
};
So what is going on there?
We register the callback with an instance of async-guard, and contextualize it by userName (which is a string, but we
could use any object). When the add
method returns false
, it means this callback wasn't the first to be added to the
list for this userName. That means we do not have to continue with our function execution.
Only the first time (when add
returned true
) will we execute httpsPost
. Once that asynchronous operation returns,
we call callbacks.run
for the userName
context, and pass the arguments that we want to pass along to all the
callbacks that have been registered.
Indeed, all callbacks that were passed in will be called. We're not just calling the first, or the last... We will
call every single callback that was added through callbacks.add()
, regardless of add
returning true
or false
.
That can be a pitfall, or a blessing. That depends on your use case.
The third and final argument that we're passing (console.error
) will be called if any of the callbacks threw an
exception. This way we can log errors. But rest assured, whether or not a callback throws, all callbacks will be
executed.
After all callbacks have executed, they are all forgotten, so they will never run again. This follows the principle we support that a callback should always be counted on to run exactly once.
MIT, enjoy!
FAQs
Protect your async operations from hammering
The npm package async-guard receives a total of 55 weekly downloads. As such, async-guard popularity was classified as not popular.
We found that async-guard demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.