I think everything is in my title, I'm always !@#$%^&* stuck with those kind of callback chains in my codes and call to external libraries. Of course as far as it stays synchronous everything is fine, when it comes to asynchronous calls ... headache comes !
I tried to do some obvious an simple samples, thanks.
var B;
(function(cb) {
cb(function(cb){
cb(function(cb){
cb();
});
});
}(function(){
setTimeout(function(){
B = 10;
}, 1);
}));
var C;
(function(cb) {
cb(function(cb){
cb(function(cb){
cb(function(){
cb(function(cb){
cb();
})
});
});
});
}(function(){
C = 10;
}));
var D = function(){
this.E = undefined;
var that = this;
(function(cb){
cb(function(cb){
cb();
});
}(function(){
setTimeout(function(){
that.E = 10;
}, 1);
}));
console.log('this.E : '+this.E);
this.F = undefined;
var that = this;
(function(cb){
cb(function(cb){
cb();
});
}(function(){
that.F = 10;
}));
console.log('this.F : '+this.F);
}
new D();
console.log('B : '+B);
console.log('C : '+C);
Result :
B : undefined
C : 10
E : undefined
F : 20