a script translatable to JavaScript with callback-hell automatic avoider :-)
- by m1uan
I looking for "translator" for JavaScript like already is CoffeScript, which will be work for example with forEach (inspired by Groovy)
myArray.forEach() -> val, idx {
// do something with value and idx
}
translate to JavaScript
myArray.forEach(function(val, idx){
// do something with value and idx
});
or something more usefull...
function event(cb){
foo()-> err, data1;
bar(data1)-> err, data2;
cb(data2);
}
the method are encapsulated
function event(cb){
foo(function(err,data1){
bar(data1, function(err, data2) {
cb(data2);
});
});
}
I want ask if similar "compiler" to JavaScript like this even better already doesn't exists?
What would be super cool... my code in nodejs looks mostly like this :-)
function dealer(cb){
async.parallel([
function(pcb){
async.watterfall([function(wcb){
first(function(a){ wcb(a); });
}, function(a, wcb){
thirt(a, function(c){ wcb(c); });
fourth(a, function(d){
// dealing with “a” as well and nobody care my result
});
}],
function(err, array_with_ac){
pcb(array_with_ac);
});
}, function(pcb){
second(function(b){ pcb(b);});
}], function(err, data){
cb(data[0][0]+data[1]+data[0][1]); // dealing with “a” “b” and “c” not with “d”
});
}
but, look how beautiful and readable the code could be:
function dealer(cb){
first() -> a;
second() -> b;
third(a) -> c; // dealing with “a”
fourth(a) -> d; // dealing with “a” as well and nobody care about my result
cb(a+b+c); // dealing with “a” “b” and “c” not with “d”
}
yes this is ideal case when the translator auto-decide, method need to be run as parallel and method need be call after finish another method. I can imagine it's works
Please, do you know about something similar? Thank you for any advice;-)