Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not support yield undefind? #119

Closed
guileen opened this issue May 9, 2014 · 2 comments
Closed

Why not support yield undefind? #119

guileen opened this issue May 9, 2014 · 2 comments

Comments

@guileen
Copy link

guileen commented May 9, 2014

I offen get this error

Error: yield a function, promise, generator, array, or object?

And it is hard to trace the problem, mostly it is happen when I yield a undefined value, so why not just allow to yield undefind.

Below code explain a case that how it happen.

function load(x){
   return function(callback) {
     db.load(x, callback);
   }
}
function loadFields(obj) {
   if(!obj) return;
   obj.x = load(obj.x);
   obj.y = load(obj.y);
   obj.z = load(obj.z);
   return obj;
}

co(function*() {
  var obj = yield getFromDb(id) // should return {x:1, y: 2, z:3};
  obj = yield loadFields(obj); // If obj is undefind, this line will occurs a Error, and hard to trace to this line.
})()
@juliangruber
Copy link
Contributor

you should rather check for obj:

var obj = yield getFromDb(id);
if (obj) obj = yield loadFields(obj); 

The way it currently is you at least know where something's wrong, swalling the error is way more obscure

@tj
Copy link
Owner

tj commented May 9, 2014

it would be much harder to track down if we did allow yield undefined, I'd probably only use object/array support in the co call itself, otherwise it's a little unclear what the intent is since it's such a sugary feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants