
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
synchronized
Advanced tools
Ensure that some code always executes exclusively, in the order it is called
This is particularly useful if you have some asynchronous code that should not be executing concurrently, e.g. where the next call relies on the result of the previous call. (Also see last example below.)
var synchd = require('synchronized');
MyObject.prototype.fetchedDocument = function(id, cb) {
var self = this;
synchd(self._documentCache, function(done){
// Return cached response if available
if (self._documentCache[id]) {
return done(null, self._documentCache[id]);
}
// Otherwise fetch
Model.findById(id, function(err, document){
// ...
self._documentCache[id] = document;
done(null, document);
});
}, cb);
}
A factory for when you're passing multiple asynchronous functions to a flow-control library method, such as async.parallel(), and you have a dependency or other reason for wanting a subset of the executed functions not be called concurrently.
async.parallel({
company: synchd.fn(this.companyId, function(done){
person.fetchedCompany(done);
}),
// ... Other functions
companyManager: synchd.fn(this.companyId, function(done){
person.fetchedCompany(function(err, company){
if (err) etc.
done(null, company.manager);
});
})
}, function(err, results){
if (err) etc.
// ...
});
For the case of having a local cache to some expensive asynchronous call.
var myObj = { id: 'abc', localCache: null, localCachedAt: null };
myObj.lookup = synchd.cachedFn(function scopeLookup() {
return this;
}, function cacheLookup(cb, cont) {
// Return cached response if available
if (this.localCachedAt) return cb(null, this.localCache);
cont();
}, function remoteLookup(cb) {
var self = this;
// Otherwise fetch
Model.findById(self.id, function(err, document){
// ....
self.localCachedAt = new Date();
self.localCache = document;
done(null, document);
});
})
myObj.lookup(function(err, document){ console.log(myObj.localCachedAt) });
myObj.lookup(function(err, document){ console.log(myObj.localCachedAt) });
Prints:
Tue Sep 16 2014 15:17:35 GMT+0100 (BST)
Tue Sep 16 2014 15:17:35 GMT+0100 (BST)
function(done) { process.nextTick(done) }null (global)Returns:
null (global)Returns:
Note: if newFn is called in context of an object, this will be the context fn and cacheLookupFn.
FAQs
Ensure that some code always executes exclusively, in the order it is called
We found that synchronized 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.