4

I can't figure out how to convert async await functionality in a while loop to a promise based implementation.

repl showing the async await version

https://repl.it/repls/IdealisticPepperyCoding

var dependency = false;

function checkDependency() {
  return new Promise(resolve => {
    setTimeout(() => {
      dependency = true;
      return resolve();
    }, 1000)
  });
}

async function isReady() {
    while(!dependency) {
      console.log('not loaded');
      await checkDependency();
    }
    console.log('loaded')
}

isReady();
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Zack Lucky
  • 671
  • 3
  • 10
  • 25
  • 1
    What did you try that didn’t work – Ry- Mar 05 '18 at 20:25
  • Removed async await and tried checkDependency().then() – Zack Lucky Mar 05 '18 at 20:30
  • 1
    Uh, async-await *is* promise-based, so I'm not sure what you're asking about? Does the code work or not, what's the problem? – Bergi Mar 05 '18 at 20:38
  • Btw, your `checkDependency` function should resolve the returned promise with the boolean value, not mutate some global `dependency` variable. – Bergi Mar 05 '18 at 20:39
  • I'm guessing this is far from "actual code", because there are about 100 better ways to write this :p – Jaromanda X Mar 05 '18 at 20:57
  • @ZackLucky, it seems that you have a stream for `false/true` values. You could consider using [RxJS stream extensions](https://rxjs-dev.firebaseapp.com/). – AlexMelw Mar 03 '20 at 10:28

3 Answers3

6

If I'm understanding you correctly, you're wanting to use Promises without async functions instead of Promises with async functions, and the sample checkDependency may actually not set dependency = true in all cases, so you want to "loop."

Equivalent functionality could look something like this, where you "recursively" call the check function. It won't actually be recursive and lead to a stack overflow, though, since the call is done in an async callback:

var dependency = false;

function checkDependency() {
  return new Promise(resolve => {
    setTimeout(() => {
      dependency = true;
      return resolve();
    }, 1000)
  });
}

function isReady() {
  if (!dependency) {
    return checkDependency().then(isReady);
  }

  return Promise.resolve(true);
}

isReady().then(() => {
  console.log('loaded')
});
Jacob
  • 77,566
  • 24
  • 149
  • 228
4

You don't need the while loop.

checkDependency().then(() => { console.log('loaded');});
Jelmer Jellema
  • 1,072
  • 10
  • 16
  • My fault the `dependency = true` in the promise isn't always true in which case it needs to check again when it is – Zack Lucky Mar 05 '18 at 20:33
  • 1
    @ZackLucky, it seems that you have a stream for `false/true` values. You could consider using [RxJS stream extensions](https://rxjs-dev.firebaseapp.com/). – AlexMelw Mar 03 '20 at 10:26
0
  • You don't need to return recolve() just call it.
  • Call the function then within the function isReady

function checkDependency() {
  return new Promise(resolve => {
    setTimeout(resolve, 1000);
  });
}

function isReady() {
  console.log('not loaded');
  checkDependency().then(() => {
    console.log('loaded')
  });
}

isReady();
Ele
  • 33,468
  • 7
  • 37
  • 75