3

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?

asked Feb 24, 2015 at 7:24
2
  • Which redis package are you using? I would guess that you are using the regular redis npm module, which expects a callback. try co-redis which returns a promise. Commented Feb 24, 2015 at 15:05
  • oh, no I am using coRedis, the code for the connection is the following: var redis = require("redis"), coRedis = require("co-redis"), db = redis.createClient(), dbCo = coRedis(db); module.exports = dbCo; (in a separate file) Commented Feb 25, 2015 at 3:40

2 Answers 2

3

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')
}
answered Feb 27, 2015 at 21:35
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the clarification. So I should never use yield in this case? What if instead I would like to do a var res = get('foo'); this.body=res? Don't I need the yield on the call, too?
You need to yield asynchronous functions (which is most of Redis' API). I think that save being synchronous (a special case) is what's confusing you. Since Redis' GET is async, then you would yield it: var res = yield redis.get('foo'). I only knew that redis.save was synchronous because it says it in bold letters on the documentation page: redis.io/commands/save. It's kind of like the difference between Node's var text = fs.readFileSync('README.md') (sync) vs fs.readFile('README.md', function(text) { ... }) (async). Does that make more sense?
0

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 }
answered Feb 24, 2015 at 8:04

1 Comment

Thanks for the answer. the function call is actually inside a generator function, just forgot to write it. Please check my edit.

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.