最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check for error when using yield instead of node-style callback? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Dec 30, 2013 at 20:13 Geert-Jan asked Dec 30, 2013 at 19:41 Geert-JanGeert-Jan 18.9k19 gold badges81 silver badges145 bronze badges 3
  • That link is going no where. Do you mean github./koajs – rene Commented Dec 30, 2013 at 19:55
  • meant: koajs.. edited – Geert-Jan Commented Dec 30, 2013 at 20:13
  • 3 just use try/catch. You get to think synchronously again, enjoy =) – Frances McMullin Commented Dec 30, 2013 at 20:23
Add a ment  | 

2 Answers 2

Reset to default 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 piler 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.

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.

发布评论

评论列表(0)

  1. 暂无评论