![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
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.
This is a small javascript function to rescue you from infinite callbacks.
Old days, you might write something like this:
ajaxGet('http://something',function(a)
{
saveToDatabase(a,function(err)
{
if (!err)
{
notifyUser(123,function()
{
markNotified(123,function()
{
//done
});
});
}
})''
});
The traditional callback style is no longer suitable for heavy event driven style programming.
Now, see what JSChain can do:
new JSChain(
{
foo: function(next)
{
console.log('foo');
setTimeout(next,1000);
},
bar: function(a,b,next)
{
console.log('bar: '+a+' '+b);
setTimeout(next,1000);
}
}).foo().bar('hello','world').exec(function(next)
{
console.log('cumtome function');
setTimeout(next,1000);
}).exec(function()
{
console.log('done');
});
With the help of JSChain, the first demo code could be written like this:
var myController =
{
ajaxGet: function(next) { ... ... next(); ... },
saveToDatabase: function(a,next){ ... ... next(); },
notifyUser: function() { ... arguments[arguments.length-1].call(); ... },
markNotified: function(next){ ... next(); ... }
};
new JSChain(myController).ajaxGet().saveToDatabase().notifyUser().markNotified();
And JSChain is very useful for data scraping projects:
function getURL(url,next)
{
console.log('getting '+url);
setTimeout(function()
{
console.log('done');
next();
},1000);
}
var chain = new JSChain({getURL: getURL});
for(var i=1;i<100;i++)
{
chain.getURL('http://example.com/'+i);
}
The JSChain itself is very tiny. The full documented source code of JSChain is only 1.6KB. I can paste the source code here, so you can read the source code. This could help you understand how it works.
function JSChain(obj)
{
var self = this;
this.____items = []; //this remembers the callback functions list
this.____next = function() //execute next function
{
var func = self.____items.shift();
if (func) func.call();
};
this.exec = function() //support for custom function
{
var args = [].slice.call(arguments,1);
args.push(self.____next);
var func = arguments[0];
self.____items.push(function()
{
func.apply(obj,args);
});
return self;
};
//copy and wrap the methods of obj
for(var func in obj)
{
if (typeof obj[func] == 'function' && obj.hasOwnProperty(func))
{
(function(func)
{
self[func] = function()
{
var args = [].slice.call(arguments); //change arguments as an array
args.push(self.____next); //pass next callback to the last argument
self.____items.push(function() //wrap the function and push into callbacks array
{
obj[func].apply(obj,args);
});
return self; //always return JSChain it self like jQuery
}
})(func); // this is the closure trick
}
}
//start execute the chained functions in next tick
setTimeout(function()
{
self.____next();
},0);
return this;
}
Good luck.
FAQs
Chain style sequentially programming for Javascript asynchronous jobs.
The npm package jschain receives a total of 12 weekly downloads. As such, jschain popularity was classified as not popular.
We found that jschain 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.