Is there a Javascript instruction to avoid the async callback hell? Similar to "if then else" I imagine a "do then". Example, suppose asyncFunc1(cb, errCb) is an async function that calls cb() on success and errCb() on failure. The same for asyncFunc2, asyncFunc3, etc. With a proper async instruction I imagine to do something like:
do (asyncFunc1) {
// error code for failure of asyncFunc1 goes here
} then (asyncFunc2) {
// error code for failure of asyncFunc2 goes here
} then (asyncFunc3) {
// error code for failure of asyncFunc3 goes here
} // etc...
var a=Math.abs(-1); // first synchronous code. This, and all the
// following code, is executed at the same time of
// the "do (asyncFunc1) { } then ... {}" block
asyncFunc1 is called when "do (asyncFunc1)" is executed, on error the code inside the brackets is executed, on success "asyncFunc2" is executed. Then again, on error the code inside the bracket is executed, on success "asyncFunc3" is executed. All this is executed at the same time of "var a=Math.abs(-1);" and of all its successive code.
Promises already are near, but, this would be cleaner: no need to define new functions and no pyramid shape.
Note1: the above example is the same as:
asyncFunc1(
function() {
asyncFunc2(
function() {
asyncFunc3(
// etc...
function() {
var a=Math.abs(-1); // first call of a synchronous function here
},
function() {
// error code for failure of asyncFunc3 goes here
}
)
},
function() {
// error code for failure of asyncFunc2 goes here
}
)
},
function() {
// error code for failure of asyncFunc1 goes here
}
)
Note2: if asyncFunc1 has arguments arg1, arg2, ... one would do:
do(asyncFunc1, arg1, arg2, ...) { }
Thats what you can already do with js:
Note that asyncFunc[2,3] must be really async...