Alkis Kalogeris
Alkis Kalogeris

Reputation: 17773

Generator without yield

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?

Upvotes: 7

Views: 3417

Answers (2)

Marinos An
Marinos An

Reputation: 10910

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

Upvotes: 0

Bergi
Bergi

Reputation: 665584

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.

Upvotes: 5

Related Questions