coolpasta
coolpasta

Reputation: 755

Can I control the flow of a function with a Promise's resolve / reject?

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?

Upvotes: 0

Views: 102

Answers (2)

Olufemi Adesina
Olufemi Adesina

Reputation: 129

Promise.all() is your best bet on the function that is returning your project.

Upvotes: 0

Bergi
Bergi

Reputation: 664185

Just don't use if (…) { …; return; } …, but if (…) { … } else { … }:

const recursively_bridge_component_start = () => bridge_component_start_recursive_callback().then(response => {
  if (response.done_batching) {
    console.log('Done batching!');
    resolve(response);
  } else {
    fake_batching_counter++;
    recursively_bridge_component_start();
  }
}, reject);

That said, you should also avoid the Promise constructor antipattern, which would sidestep your problem completely:

let fake_batching_counter = 0;
function run_batch() {
  fake_batching_counter++;
  console.log('Running a batch...');
  return new Promise((resolve, reject) => {
    setTimeout(() => { // at least simulate asynchrony
      resolve({
        'done_batching': fake_batching_counter == 3
      });
    }, 1000);
  });
}

function run_recursively() {
  return bridge_component_start_recursive_callback().then(response => {
    if (response.done_batching == true) {
      console.log('Done batching!');
      return response;
    } else {
      return run_recursively();
    }
  })
}

const bridging_result = run_recursively();

Upvotes: 1

Related Questions