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?
2 Answers 2
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')
}
2 Comments
var res = get('foo'); this.body=res? Don't I need the yield on the call, too?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?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 }
var redis = require("redis"), coRedis = require("co-redis"), db = redis.createClient(), dbCo = coRedis(db); module.exports = dbCo;(in a separate file)