I understand the difference between a return
and a resolve
but I feel like I'm littering my code with return
where I need the Promise to either immediately resolve or reject based on logic.
Here's my actual code:
function bridge_component_start_in_batch_test() {
let fake_batching_counter = 0;
function bridge_component_start_recursive_callback() {
console.log('Running a batch...');
return new Promise((resolve, reject) => {
if (fake_batching_counter == 3) {
resolve({
'done_batching': true
});
}
resolve({
'done_batching': false
});
});
}
const bridging_result = new Promise((resolve, reject) => {
const recursively_bridge_component_start = () => bridge_component_start_recursive_callback().then(response => {
if (response.done_batching == true) {
console.log('Done batching!');
resolve(response);
return (response);
}
fake_batching_counter++;
recursively_bridge_component_start();
}).catch(error => {
reject(error);
return (response);
});
recursively_bridge_component_start();
});
return bridging_result;
}
bridge_component_start_in_batch_test().then(result => {
console.log(result);
});
If I remove the return statements, I enter an infinite loop and rightfully so.
I understand that the return
is used to stop the running of the recursively_bridge_component_start
and it has nothing to do with the promise itself, but this feels extremely conflictual.
Is there no way to somehow re-write this so that I don't have to litter everything with return statements?