title: Promises: roll dice async
body: <p>I was trying to transform this code:</p>
  <pre><code>function rollDie() {
    var random = prngInt()
    var dieVal = (random % 6) + 1
    var reroll = random &gt; 251
  
    if (reroll) {
      return rollDie()
    } else {
      return dieVal
    }
  }
  </code></pre>
  <p>Into async function using promises. The problem comes when I try to adapt this if else statement but I'm new to JavaScript and async programming.
  I've started doing this (Promise function that I will use):</p>
  <pre><code>function getPseudoRandomInt() {
      function promiseBehaviour(resolve, reject) {
          setTimeout(() =&gt; resolve(prngInt()), 1000)
      }
      return new Promise(promiseBehaviour);
  }
  </code></pre>
  <p>After that, I've tried to translate/adapt directly but isn't working.</p>
  <p>My try:</p>
  <pre><code>function rollDieAsync() {
    var random = getPseudoRandomInt()
    var dieVal = random.then(x =&gt; (x % 6) + 1)
    var reroll = random.then(x =&gt; x) &gt; 251
  
    if (reroll) {
      return rollDieAsync()
    } else {
      return dieVal
    }
  }
  }
  </code></pre>
  <p>This function is working but not the way I want, because if statement will not execute, since re-roll promise won't get evaluated.</p>
  <p>I would like some help to do it but in the simplest way.</p>