title: How to return from yield *
body: <p>Is there any way to return from a <code>yield * asyncIterator</code>?</p>
  
  <p><strong>Background:</strong></p>
  
  <p>Given an async iterator like this:</p>
  
  <pre class="lang-js prettyprint-override"><code>const asyncIterator = {
    next () {
      return new Promise(resolve =&gt; setTimeout(() =&gt; resolve('next'), 500))
    },
    return () {
      return Promise.resolve('return')
    },
    [Symbol.asyncIterator] () {
      return this
    }
  }
  </code></pre>
  
  <p>the following will never invoke its <code>return</code> method:</p>
  
  <pre class="lang-js prettyprint-override"><code>async * yieldFromIterator () {
    yield 'start'
    yield * asyncIterator
  }
  
  const yieldingIterator = yieldFromIterator()
  console.log(await yieldingIterator.next()) // 'start'
  console.log(await yieldingIterator.next()) // 'next'
  console.log(await yieldingIterator.return()) // never resolved
  </code></pre>
  
  <p>neither does this:</p>
  
  <pre class="lang-js prettyprint-override"><code>async * yieldFromIterator () {
    yield 'start'
    for await (const result of asyncIterator) {
      yield result
    }
  }
  
  const yieldingIterator = yieldFromIterator()
  console.log(await yieldingIterator.next()) // 'start'
  console.log(await yieldingIterator.next()) // 'next'
  console.log(await yieldingIterator.return()) // never resolved
  </code></pre>
  
  <p>while the following of course does:</p>
  
  <pre class="lang-js prettyprint-override"><code>async * returnIterator () {
    yield 'start'
    return asyncIterator
  }
  
  const returningIterator = returnIterator()
  console.log(await returningIterator.next()) // 'start'
  console.log(await returningIterator.next()) // 'next'
  console.log(await returningIterator.return()) // 'return'
  </code></pre>
  
  <p>Thank you very much :)</p>