Here is my sample code.
Orchestrator calls the worker with couple of inputs first, upon getting the response, it has to validate whether the response was satisfactory or not.
If satisfactory, just return to the caller.
If not, again, call the same worker or may be different worker with slightly different input and follow the flow.
Here, although, my code calls cb() after the first worker call, it's also going to the second then and errors out "response" is undefined etc..
I could add an extra condition to check whether the 1st response was satisfactory, like need2ndworkercall && validate(response) in 2nd then and get away with it. But wondering what is the right way of dealing with this problem. Appreciate any feedback.
function orchestrateSomething(input, cb){
doSomething(input.a, input.b)
.then(response=>{
if(validate(response)){
cb(buildResultObj(response));
}
else{
return doSomething(input.a)
}
})
.then(response=>{
if(validate(response)){
cb(buildResultObj(response));
}
else{
cb(null,{});
}
})
.catch(error=>cb(error));
}