I am building a simple charting tool. When a user chooses a data source and a chart type, both the data and the script for the specific chart (.js) are loaded and then the code
I built something like this a while back that I called "when-then". I wanted something that would say, "Load all of these things, then do something when they're done".
https://github.com/geuis/when-then
I was inspired by the promises idea, but wanted something a little simpler for simple tasks.
Check Deferred it has modular build, and you can decide just to take core and that shouldn't be larger than 1kb.
Browser install instructions explains how to create such bundle in few easy steps
If you want it really lightweight, You should try sb-promise, It's under MIT license. It's just a few kbs and is API compatible with native Promises.
function myFunction(){
return new Promise(function(resolve,reject){
resolve({some:"data"});
}
}
myFunction().then(function(result){
console.log(result); // {some:"data"}
});
I strongly suggest using an implementation of the Promises/A specification, which is becoming the standard way of doing promises in JavaScript. Promises work better when everyone uses the same flavor, so it's in everyone's interest to use compatible implementations.
Q is probably the most popular and fully featured implementation (it also will adapt jQuery and other incompatible promises), while when and rsvp are supposed to be more "lightweight".
Wanted to throw in my 2¢ as there's been a development here. Promises have arrived natively in JavaScript and will be implemented for FF 30 and Chrome 33 (according to this table).
Although that's hardly worth mentioning in a place that's usually flooded with "but does it support IE6?", there's a 2kb minified and gzipped polyfill here that is "basically an API remapping of rsvp.js."
Since jQuery promises aren't really promises, and I agree with this guy that promise syntax is inconsistent across libraries, my view is that native promises are already worth implementing even though your users' browsers probably don't support them yet.
Edit: After using Promises in the context of Angular and the Firefox addon-SDK, both of which are very similar to Q, I prefer that syntax and have read that it is still faster than the native implementation.
Try async.
It has methods to do operations in parallel, and in series. It was originally designed to run in node, but now works in browser as well.
EDIT TO ADD: They don't have a minified version, so the full enchilada is ~42kb.