john doe
john doe

Reputation: 111

Better understanding javascript's yield

I have the following code in my Koa app:

exports.home = function *(next){
  yield save('bar')
}

var save = function(what){
  var response = redis.save('foo', what)
  return response
}

But I get the following error: TypeError: You may only yield a function, promise, generator, array, or object, but the following object was passed: "OK"

Now, "ok" is the response from the redis server, which makes sense. But I cannot fully grasp the concept of generators for this kinds of functions. Any help?

Upvotes: 3

Views: 2078

Answers (2)

danneu
danneu

Reputation: 9464

You don't yield save('bar') because SAVE is synchronous. (Are you sure you want to use save?)

Since it's synchronous, you should change this:

exports.home = function *(next){
  yield save('bar')
}

to this:

exports.home = function *(next){
  save('bar')
}

and it will block execution until it's finished.

Almost all other Redis methods are asynchronous, so you would need to yield them.

For example:

exports.home = function *(next){
  var result = yield redis.set('foo', 'bar')
}

Upvotes: 3

255kb - Mockoon
255kb - Mockoon

Reputation: 6984

Yield is supposed to be used inside a generator function according to the documentation. The purpose is to return the result of an iteration to be used in the next iteration.

Like in this example (taken from the documentation):

function* foo(){
  var index = 0;
  while (index <= 2) // when index reaches 3, 
                     // yield's done will be true 
                     // and its value will be undefined;
    yield index++;
}

var iterator = foo();
console.log(iterator.next()); // { value:0, done:false }
console.log(iterator.next()); // { value:1, done:false }
console.log(iterator.next()); // { value:2, done:false }
console.log(iterator.next()); // { value:undefined, done:true }

Upvotes: 0

Related Questions