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

Added api to pass errors from generators #7

Merged
merged 3 commits into from
Jun 26, 2016
Merged

Conversation

KoryNunn
Copy link
Owner

No description provided.


reject('foo');

var x = yield righto(function(done){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var y

@KoryNunn
Copy link
Owner Author

I have some reservations about the PR straight away, but I'm not sure if they are valid.

calling reject() does not necessarily halt execution at that point, but rather, will allow execution to continue to the next yield or return. This could cause side effects, just as it would if you allowed an errback style function to call callback(error) without returning.

I could solve this by throwing within the reject function, catching all throw errors, and detecting ones thrown by reject, swallowing, and passing the error, while re-throwing valid throws, but that would have a substantial impact on performance.

@bguiz
Copy link
Contributor

bguiz commented Jun 26, 2016

I think it isn't worth the overhead. Also if you're using standard callbacks (without righto), you would already be accustomed to returning if the callback isn't at the last executable line of of the current function scope.

});
});

reject('foo');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps change this to

if (x === 'x') { 
  return reject('foo');  
}

@bguiz
Copy link
Contributor

bguiz commented Jun 26, 2016

So see the diff comment above, so the test now demonstrates that a return is expected, just like you would if a callback had occurred at that point.

Additional observation: Since reject takes the first argument as an error, perhaps we could just make it into a standard errback, so that 2nd arg is considered the result. So it could also be used to shortcut the iteration of the generator function. The return achieves this anyway, but would make for a more consistent experience with errbacks.

@KoryNunn
Copy link
Owner Author

I had coincided allowing to pass results there, but that does add ways to do the same task, which I'm not a huge fan of. It also wouldn't be a breaking change too add later, so, goat tracks and all that...

@bguiz
Copy link
Contributor

bguiz commented Jun 26, 2016

but that does add ways to do the same task

'nuff said... merge!

@KoryNunn KoryNunn merged commit 5ca98d2 into master Jun 26, 2016
@KoryNunn KoryNunn deleted the passed-generator-errors branch June 26, 2016 11:16
KoryNunn added a commit that referenced this pull request Nov 15, 2019
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

Successfully merging this pull request may close these issues.

2 participants