question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unclear magic using "return"

See original GitHub issue

I got a bit confused following the explanation in http://exploringjs.com/es6/ch_iteration.html#_closing-iterators-via-return

The issue is related to the functions taking as input an iterable and returning an iterable: map, filter, slice and many others.

When you stop consuming one of the iterables returned by those, you can call “return”, as specified in the link above (or rely on for…of that is going to do that automatically). The problem is that I don’t see how we are able to close the previous iterable calling return on that.

I wrote this bit of code to verify my doubt:

const slice = require('./es2018/slice')

const inf = {
  next: () => ({ done: false, value: 'x' }),
  return: () => { // who is calling this ?
    console.log('close')
    return { done: true }
  }
}
 
const iter = {
  [Symbol.iterator]: () => inf
}

for (const i of slice(3, iter)) {
  console.log(i)
  break // this should fire "return" on the generator object created by slice
}

// x
// close

The good news is that, despite my concern it works fine (it calls return) I didn’t understand how this magic works though.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
conartist6commented, Aug 2, 2018

I’ve figured out what’s going on with Babel failing. It was unpleasantly difficult. The problem is with slice, not with zip. Long story short, there’s a limitation in transform-regenerator. That limitation is described here: http://babeljs.io/docs/en/babel-plugin-transform-es2015-for-of/#options-loose. We have the loose option enabled:

      "presets": [
        ["@babel/preset-env", {
          "loose": true
        }]
      ]

Turning that option off unbreaks this test case at the cost of performance in the es5 code. I’d say we have no choice, the library code is only correct with strict on.

0reactions
conartist6commented, Aug 2, 2018

Also TIL that you can write return yield* ....

Read more comments on GitHub >

github_iconTop Results From Across the Web

Willing magic or witchcraft always return to the sender, or is ...
When a person's black Magic effects is removed, by a competent tantric, generally it is returned with thanks to the sender, here the...
Read more >
How exactly does the ”return target spell you don't control ...
Hi all - I am hoping to get your input on the use of a spell - Magic Circle. This spell can be...
Read more >
Six Consequences of Poorly Thought-Out Magic Systems
If by some chance the story makes it to publication, readers will either abandon the inscrutable text or come away with the wrong...
Read more >
What Are Magic Numbers And Why Are They Bad
A brief explanation of magic numbers in code and why they are bad.
Read more >
What Is a Magic Number And How Do We Fix It?
Removing Magic Numbers From Code​​ The trick is to take the seemingly random value and give it some context by providing it a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found