How to check for error when using yield instead of node-style callback?
Asked Answered
F

2

13

I'm wrapping my head around the new ecma6 generators and yield-operator in javascript, specifically in the context of koa.

Consider the contrived example:

  newUser.save(function(err, user) {
    if(err){
      //do something with the error
    }
    console.log("user saved!: " user.id);
  }

'Yieldified' this would look something like this:

  var user = yield newUser.save();
  console.log("user saved!: " user.id);

But how would I check for err to exist, with the purpose of executing //do something with the error?

Frowzy answered 30/12, 2013 at 19:41 Comment(3)
That link is going no where. Do you mean github.com/koajsSclater
meant: koajs.com. editedFrowzy
just use try/catch. You get to think synchronously again, enjoy =)Am‚lie
C
13

Unfortunately generators suck for error handling. I mean checking errors manually on every step of the way and propagating them manually sucks too but not as much as the try-catch statement in Javascript.

   try {
       var user = yield newUser.save();
       console.log("user saved!: " user.id);
   }
   catch (e) {
       //Abstract code that checks if the error is what you think it is
       if (isFromNewUserSave(e)) {

       }
       else {
           throw e;     
       }
   }

The problem with try catch statement as you can see is that it catches everything. There is an additional problem in that errors that would be compiler errors in other languages are thrown in Javascript at runtime. But if you just use try catch without checks you will not see them at all.

Concepcion answered 2/1, 2014 at 16:54 Comment(0)
J
-1

You should check your generator function. There's too few context to assertive recognize your problem with Exception/Error Handling... however, I've identified a behavior with try/catch:

*Tested on Firefox 33.1.1 and Chrome 39.0.2171.65 m

This is the WRONG way to declare a generator function (without *), and it seems it affects the Error Handling behavior:

function wrongGenerator()
{
  for(var i = 0; i <= 0 ; i++)
  {
    if(i < 3)
      yield i;
  }
}

try
{
  var gen = new wrongGenerator();

  gen.next();
  gen.next();
  gen.next();
  gen.next();

  throw new Error("Test");
}
catch(e)
{
  //This should return an Error Object, but it just don't.
  console.log(e);
  console.log(e instanceof Error);
}

On the other hand, when you declare a generator function the right way, error handling works just nicely:

function* rightGenerator() {
  for(var i = 0; i <= 1; i++)
  {
    if(i < 3)
      var a = yield i;
  }
}

try
{
  var gen = new rightGenerator();

  gen.next();
  gen.next();
  gen.next();
  gen.next();

  throw new Error("Test");
}
catch(e)
{
  //Returns an Error Object, as expected.
  console.log(e);
  console.log(e instanceof Error);
}

Not sure if this is an issue on Node environment, but I think it could answer partially your concern.

Jahdai answered 26/11, 2014 at 0:51 Comment(1)
Um, if you declare a generator function (that uses yield) without the * and don't get a snytax error from that, then something else is very wrong.Darlleen

© 2022 - 2024 — McMap. All rights reserved.