7
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?

asked Dec 21, 2015 at 21:44
3
  • 2
    Maybe whatever calls demo expects an iterator to be returned. A generator function is a simple way to create an iterator. Commented Dec 21, 2015 at 22:06
  • Yes that's a good use case. Have you ever used for any other cases? Commented Dec 21, 2015 at 22:20
  • @FelixKling If you want you can post your comment as an answer so I can accept it. Commented Dec 22, 2015 at 8:36

2 Answers 2

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.

answered Dec 22, 2015 at 12:32
Sign up to request clarification or add additional context in comments.

Comments

0

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

answered Feb 21, 2019 at 20:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.