Unclear magic using "return"
See original GitHub issueI 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:
- Created 5 years ago
- Comments:10 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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: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.Also TIL that you can write
return yield* ...
.