admin管理员组文章数量:1415684
var A = {
demo : function() * {
/* Some logic here, but no yield is used */
}
}
What is the use of a generator
method that does not yield
anything?
Have you ever used something like this? What was the use case?
var A = {
demo : function() * {
/* Some logic here, but no yield is used */
}
}
What is the use of a generator
method that does not yield
anything?
Have you ever used something like this? What was the use case?
-
2
Maybe whatever calls
demo
expects an iterator to be returned. A generator function is a simple way to create an iterator. – Felix Kling Commented Dec 21, 2015 at 22:06 - Yes that's a good use case. Have you ever used for any other cases? – Alkis Kalogeris Commented Dec 21, 2015 at 22:20
- @FelixKling If you want you can post your ment as an answer so I can accept it. – Alkis Kalogeris Commented Dec 22, 2015 at 8:36
2 Answers
Reset to default 5It's quite the same case like an empty function - someone wants to call a function, but you have nothing to do.
Similarly, an empty generator function is a function which creates a generator that does nothing. It does represent the empty sequence. However, a generator function that doesn't yield
isn't necessarily empty - it can still do something and have a result value, but there simply are no intermediate results.
The following code prints 'someValue' on the response every 100 ms for 5 seconds. It does not use yield
.
const Koa = require('koa');
const through = require('through');
(new Koa()).use(function *(){
const tr = through();
setInterval(() => tr.write('someValue\n'), 100);
setTimeout(tr.end, 5000);
this.body = tr;
}).listen(3003, () => {});
Access with: curl localhost:3003
本文标签: javascriptGenerator without yieldStack Overflow
版权声明:本文标题:javascript - Generator without yield - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745239325a2649229.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论