0

I'm having issues with this block of code while attempting to make it non-blocking.

The code is intended to wait for an undefined variable to be assigned in a non-blocking manner. I have attempted using the statements in the if block without success.

The issues I'm having are;

  • Program does not wait (await) until the checkInputCoinStatus() function is done
  • The whole program ends unexpectedly after the variable has been set

I would like to wait for the for all processes within checkInputCoinStatus() to be finished before proceeding inside a main function that is asynchronous.

async checkInputCoinStatus() {

  // while (true) {
  //   if (this.selectedCoin) {
  //     break;
  //   }
  //   await new Promise((r) => setTimeout(r, 1));
  // }

  const getSelectedCoin = () => {
    return this.selectedCoin
  }

  await (async function codeBlock() {
    const selectedCoin = getSelectedCoin()
    if (selectedCoin === undefined) {
      // setTimeout(async () => {await codeBlock()}, 0)
      // await new Promise(() => setTimeout(async () => {await codeBlock()}, 0));
      // await new Promise(() => setTimeout(codeBlock, 0));
    }
  })();
}

Thank you.

Yomi
  • 38
  • 1
  • 4
  • Why don't you want to use `setTimeout`? – Barmar Jul 19 '21 at 18:50
  • I'm going to guess that you are trying to return something from this function? You can't, it's an async function. It can only ever return a promise. – Adam Jenkins Jul 19 '21 at 18:53
  • I have tried setTimeout (commented out) but I didn’t have luck with that. – Yomi Jul 19 '21 at 19:46
  • I don’t intend to return anything from the function either. The function should halt the program until it detects the variable has been changed – Yomi Jul 19 '21 at 19:47

0 Answers0