最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Generator without yield - Stack Overflow

programmeradmin0浏览0评论
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?

Share Improve this question asked Dec 21, 2015 at 21:44 Alkis KalogerisAlkis Kalogeris 17.8k15 gold badges62 silver badges118 bronze badges 3
  • 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
Add a ment  | 

2 Answers 2

Reset to default 5

It'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

发布评论

评论列表(0)

  1. 暂无评论