forEach 在 Node.js 中使用生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24775747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
forEach using generators in Node.js
提问by Mazhar Ahmed
I'm using Koa.jsframework and Mongoose.jsmodule.
我正在使用Koa.js框架和Mongoose.js模块。
Normally to get a result from MongoDB I code like this:
通常要从 MongoDB 获得结果,我的代码如下:
var res = yield db.collection.findOne({id: 'my-id-here'}).exec();
But I need to execute this line for every element of an array named 'items'.
但是我需要为名为“items”的数组的每个元素执行这一行。
items.forEach(function(item) {
var res = yield db.collection.findOne({id: item.id}).exec();
console.log(res) // undefined
});
But this code doesn't run as yield is in the function. If I write this:
但是此代码不会像函数中的 yield 那样运行。如果我这样写:
items.forEach(function *(item) {
var res = yield db.collection.findOne({id: item.id}).exec();
console.log(res) // undefined
});
I'm not getting the result in res variable either. I tried to use 'generator-foreach' module but that didn't worked like this.
我也没有在 res 变量中得到结果。我尝试使用“ generator-foreach”模块,但这并没有像这样工作。
I know that this is my lack of knowledge about the language literacy of Node.js. But can you guys help me finding a way how to do this?
我知道这是我对 Node.js 语言素养的缺乏了解。但是你们能帮我找到一种方法来做到这一点吗?
采纳答案by Mazhar Ahmed
Thanks guys, I've done this using the 'CO' module. Thanks.
谢谢大家,我已经使用“ CO”模块完成了这项工作。谢谢。
var co = require('co');
items.forEach(co(function* (item) {
var img = yield db.collection.findOne({id: item.id}).exec();
}));
EDIT: With the latest version of CO, you need co.wrap() for this to work.
编辑:使用最新版本的 CO,您需要 co.wrap() 才能工作。
回答by Umidbek Karimov
You can yieldarrays, so just map your async promises in another map
您可以使用yield数组,因此只需将您的异步承诺映射到另一个映射中
var fetchedItems = yield items.map((item) => {
return db.collection.findOne({id: item.id});
});
回答by Jorge Riv
The accepted answer is wrong, there is no need to use a library, an array is already an iterable.
接受的答案是错误的,不需要使用库,数组已经是可迭代的。
This is an old question, but since it has no correct answer yet and it appears on the first page on google search for the key terms "iterators and forEach" I will respond the question:
这是一个老问题,但由于它还没有正确答案,而且它出现在谷歌搜索的第一页上搜索关键术语“迭代器和 forEach”,我将回答这个问题:
There is no need to iterate over an array, since an array already conforms to the iterable API.
不需要迭代数组,因为数组已经符合可迭代 API。
inside your generator just use "yield* array" (note the * ) yield* expression is used to delegate to another generator or iterable object
在您的生成器中只需使用“yield* 数组”(注意 * )yield* 表达式用于委托给另一个生成器或可迭代对象
Example:
例子:
let arr = [2, 3, 4];
function* g2() {
yield 1;
yield* arr;
yield 5;
}
var iterator = g2();
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: 3, done: false }
console.log(iterator.next()); // { value: 4, done: false }
console.log(iterator.next()); // { value: 5, done: false }
console.log(iterator.next()); // { value: undefined, done: true }
For examples and in depth information visit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield*
有关示例和深入信息,请访问:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield*

