title: AsyncIterator exits "for await-of" loop abruptly
body: <p>I'm trying to build a <code>buffer</code> backed <code>AsyncIterator</code> to produce the values from the buffer when buffer is not empty &amp; <code>await</code> on a <code>Promise</code> if buffer is empty.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>class PushGenerator {
    constructor() {
      this.buffer = []
      this.pending = null
    }
    
    // push values to iterator. `null` will stop the iterator
    push(value) {
      if (this.pending) {
        this.pending(value)
        this.pending = null
        return
      }
      this.buffer.push(value)
    }
    
    async *[Symbol.asyncIterator]() {
      let event = null
      
      while(true) {
          if (this.buffer.length &gt; 0) event = this.buffer.shift()
        else event = await new Promise(r =&gt; this.pending = r)
        
        if (event !== null) yield event
        else break
      } 
    }
  }
  
  async function main() {
    const gen = new PushGenerator()
    setTimeout(() =&gt; gen.push(10), 10)
  
    for await (const item of gen) {
       console.log("---", item)
    }
    
    setTimeout(() =&gt; gen.push(null), 3000)
    console.log('Im not called!')
  }
  
  main().then(v =&gt; console.log('never called', v))
    .catch(e =&gt; console.log('never called', e))</code></pre>
  </div>
  </div>
  </p>
  <p>For most parts it works fine, except that if I <code>push</code> the values <strong>after</strong> the <code>await for-of</code> loop, its completely ignored. Infact the <code>async main</code> itself terminates without resolving/rejecting the promise!. My expectation is that <code>for await-of</code> loop to wait for values until loop is broken or <code>null</code> is sent to the <code>PushGenerator</code></p>
  <p>Behaviour is same both in nodejs &amp; also in chrome. Googling didnt help much &amp; not able to find any similar question in SO as well.</p>
  <p>What am I missing?</p>