# Get return value of iterators when used in for-of loops

**URL:** <https://es.discourse.group/t/get-return-value-of-iterators-when-used-in-for-of-loops/1697>\
**Category:** 💡 Ideas\
**Tags:** proposal, iterator-helpers\
**Created:** [April 21, 2023, 11:55am UTC](https://es.discourse.group/t/get-return-value-of-iterators-when-used-in-for-of-loops/1697 "2023-04-21T11:55:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthieu-penbox](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/matthieu-penbox/32/1720_2.png) [@matthieu-penbox](https://es.discourse.group/u/matthieu-penbox)\
**Post date:** [April 21, 2023, 11:55am UTC](https://es.discourse.group/t/get-return-value-of-iterators-when-used-in-for-of-loops/1697/1 "2023-04-21T11:55:14Z")

</div>

Currently, using a generator function that has a return value in a for-of loop means that we loose access to that return value.

In order to work around this, we need to explicitly manipulate the iterator, which can be annoying (e.g. we must not forget to call the `iterator.return?.()` function, which is done under the hood when using a for-of loop).

My proposal would be to allow retrieving the return value somehow. Here is an example of syntaxt that would allow this:

```js
function* generate() {
  yield 1
  return "foo"
}

const returnValue = for (const item of generate()) {
  console.log(item) // 1
}

console.log(returnValue) // foo

```

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [April 21, 2023, 12:12pm UTC](https://es.discourse.group/t/get-return-value-of-iterators-when-used-in-for-of-loops/1697/2 "2023-04-21T12:12:03Z")

</div>

You can achieve the same with:

```javascript
function* generate() {
  yield 1
  return yield "foo"
}

for(const item of generate()) {
    console.log(item)
}

// 1
// foo

```

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [April 21, 2023, 3:48pm UTC](https://es.discourse.group/t/get-return-value-of-iterators-when-used-in-for-of-loops/1697/3 "2023-04-21T15:48:21Z")

</div>

> [@jithujoshyjy](#):
>
> You can achieve the same with:  
> ...

That isn't returning "foo". Its yielding "foo" then returning the result of that expression. That could be anything depending on who's calling next.

```typescript
function* generate() {
  yield 1
  return yield "foo"
}

const gen = generate()
console.log(gen.next()) // { value: 1, done: false }
console.log(gen.next()) // { value: "foo", done: false }
console.log(gen.next("bar")) // { value: "bar", done: true }

```

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [April 21, 2023, 5:23pm UTC](https://es.discourse.group/t/get-return-value-of-iterators-when-used-in-for-of-loops/1697/4 "2023-04-21T17:23:55Z")

</div>

One potential reason to not add new semantics to get the return value from iterators would be that the return value is not passed through when using the new iterator helper methods ([proposal-iterator-helpers/issues/217](https://github.com/tc39/proposal-iterator-helpers/issues/217)) reducing the chance that iterators will have a useful `return` value.
