I'm building a web app which has a set of functions that the user may perform a few times but involve enough asynchronous actions for callbacks to get a bit out of hand.
What are realistic alternatives to $.Deffered
and $.when
that can be 'used' multiple times?
I think what you're looking for are events. An example in jQuery using on and trigger:
var data_handler = $({});
function get_data(new_foo) {
// Do stuff, then send the event
data_handler.trigger('new_data', {foo: new_foo});
}
data_handler.on('new_data', function(e, data) {
console.log('Handler 1: ' + data.foo);
});
data_handler.on('new_data', function(e, data) {
console.log('Handler 2: ' + data.foo);
});
get_data(1);
get_data(2);
Output:
Handler 1: 1
Handler 2: 1
Handler 1: 2
Handler 2: 2
Here are 3 such libraries:
These libraries are not "full blown". They just implement promises.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With