I am exploring a pattern in which function1, function2, and function3 are linked together through their callbacks.
Given that each of these functions may take up to 1 second to complete, I am interested in alternative approaches to prevent nesting from becoming unmanageable as more callback functions are added.
function1(function(cbData1){
if(cbData1){
function2(cbData1, function(cbData2){
if(cbData2){
function3(cbData2, function(cbData3){
// success
}
} else {
// failed for reason#2
}
});
} else {
//failed for reason#1
}
});
//example function
function function2(data, callback) {
// carry out necessary operations
callback(newData);
}