为什么不会从`.map`回调中返回? [英] Why won't yield return from within a `.map` callback?

查看:190
本文介绍了为什么不会从`.map`回调中返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

研讨会。



@slebetman的回答是正确的,我也可以添加更多:



是的, MDN - 迭代协议不直接引用回调中的 yield
但是,它告诉我们从产生项目的重要性,因为您只能使用 yield 发电机。请参阅 MDN - Iterables 文档以了解更多信息。



@marocchino 建议只是很好的解决方案迭代在地图之后更改的数组:

  function * upper(items){
yield * items.map(function(item){
try {
return item。 toUpperCase();
} catch(e){
return null;
}
});
}

我们可以这样做,因为Array有迭代机制,请参见 Array.prototype [@@ iterator]()

  var bad_items = ['a','B',1,'c']; 

for(let item of bad_items){
console.log(item); // a B 1 c
}

Array.prototype.map 没有默认的迭代行为,所以我们无法迭代



但是生成器不仅仅是迭代器。每个生成器都是一个迭代器,反之亦然。生成器允许您通过调用 yield 关键字来自定义迭代(而不仅仅是)进程。您可以在这里播放并查看生成器/迭代器之间的区别:



演示 babel / repl


Learn Generators - 4 » CATCH ERROR! The solution uses a for loop but I just couldn't find anything in 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:

function *upper (items) {
  items.map(function (item) {
    try {
      yield item.toUpperCase()
    } catch (e) {
      yield 'null'
    }
  }
}

var badItems = ['a', 'B', 1, 'c']

for (var item of upper(badItems)) {
  console.log(item)
}
// want to log: A, B, null, C

Error:

⇒  learn-generators run catch-error-map.js
/Users/gyaresu/programming/projects/nodeschool/learn-generators/catch-error-map.js:4
      yield item.toUpperCase() // error below
            ^^^^
SyntaxError: Unexpected identifier
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Even my editor knows this is a terrible idea...

解决方案

Disclaimer: I'm the author of Learn generators workshopper.

Answer by @slebetman is kinda correct and I also can add more:

Yes, MDN - Iteration Protocol doesn't refer directly about yield within callbacks. But, it tell us about importance from where you yield item, because you can only use yield inside generators. See MDN - Iterables docs to find out more.

@marocchino suggest just fine solution iterate over Array that was changed after map:

function *upper (items) {
  yield* items.map(function (item) {
    try {
      return item.toUpperCase();
    } catch (e) {
      return null;
    }
  });
}

We can do it, because Array has iteration mechanism, see Array.prototype[@@iterator]().

var bad_items = ['a', 'B', 1, 'c'];

for (let item of bad_items) {
  console.log(item); // a B 1 c
}

Array.prototype.map doesn't have default iteration behavior, so we couldn't iterate over 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 yield keyword. You can play and see the difference between generators/iterators here:

Demo: babel/repl.

这篇关于为什么不会从`.map`回调中返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆