Why won't yield return from within a `.map` callback? #1186
Comments
Pingin https://github.com/isRuslan |
You mean @isRuslan ? |
BTW, If you want to use function *upper (items) {
yield* items.map(function (item) {
try {
return item.toUpperCase()
} catch (e) {
return null
}
})
} |
I started a question on Stack Overflow: Why won't yield return from within a I hope that's not bad form |
Hi everyone. What a good question. Yes, MDN - Iteration Protocol doesn't refer directly about @marocchino suggest just fine solution iterate over Array that was changed after map. We can do it, because Array has iteration mechanism, see [Array.prototype@@iterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/@@iterator).
Array.prototype.map doesn't have default iteration behavior, so we couldn't iterate through it. But generators is not just iterators. Every generator is an iterator, but not vice versa. Generators allows you to customize iteration (and not only) process by calling |
Wow. @marocchino & @isRuslan Thanks so much for a fantastic solution and explanation! Closing issue. |
Should it be cross-posted to StackOverflow? But answer by slebetman is kinda correct also. |
@isRuslan I think you should definitely post a solution to SO. I just want the knowledge out there not the points :) edit: I've removed my comments on the SO post. |
Learn Generators - 4 » CATCH ERROR!
The solution uses a
for loop
but I just couldn't find anything MDN - Iteration Protocols that refers to yield within callbacks.I'm going to guess the answer is just
don't do that
but thanks in advance if anyone has the time or inclination to provide an explanation!Code:
Error:
Even my editor knows this is a terrible idea...
The text was updated successfully, but these errors were encountered: