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

Simplify rejection handler to propagate error by re-throwing reason. #554

Merged
merged 1 commit into from
Oct 27, 2015
Merged

Simplify rejection handler to propagate error by re-throwing reason. #554

merged 1 commit into from
Oct 27, 2015

Conversation

sparhami
Copy link

@sparhami sparhami commented Oct 9, 2015

No description provided.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project, in which case you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@googlebot
Copy link

CLAs look good, thanks!

@@ -132,6 +132,6 @@ function racePromise_(promise, unlisten, timeout) {
return result;
}, (reason) => {
unlisten();
return Promise.reject(reason);
throw reason;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a guarantee that reason would always be an Error?

@sparhami
Copy link
Author

sparhami commented Oct 9, 2015

Interestingly enough, you can throw anything you want in JavaScript.

@erwinmombay
Copy link
Member

throwing a string literal (strings etc, non Error) is generally bad practice though

@dvoytenko
Copy link
Contributor

Yeah, we've been trying to avoid that.
On Oct 8, 2015 9:55 PM, "erwin mombay" notifications@github.com wrote:

throwing a string is generally bad practice though


Reply to this email directly or view it on GitHub
#554 (comment).

@sparhami
Copy link
Author

sparhami commented Oct 9, 2015

By the same token you shouldn't reject a Promise with something that isn't an Error. If you converted the code to async/await, you wouldn't throw a string.

@erwinmombay
Copy link
Member

hmm thats a good point. but i generally think of promise rejection as a way to cancel all the handlers subsequent to the current one (breaking the composition) and not an analog of actual error handling.

@dvoytenko
Copy link
Contributor

Agree. Would gladly accept a PR that replaces reject(string) with reject(new Error()). But I do wonder:
(a) We often not in control of rejection reason, e.g. through some other API.
(b) The worst case, I think, is when someone does Promise.reject(), i.e. with reason=undefined.

Any thoughts on these?

@erwinmombay
Copy link
Member

@sparhami friendly ping to follow up on @dvoytenko

@sparhami
Copy link
Author

I'm not sure why it matters if you don't have control over the upstream. You can throw undefined just fine. This change does not introduce any change in behavior.

I would encourage you to change the upstream to throw an Error instead of a string. It is probably a good idea because then you can tell what actually caused the rejection chain rather than getting a string and having to search for it in the code base.

If you would like, I could create a separate PR for that change.

@dvoytenko
Copy link
Contributor

Apologies, missed the last report. This sounds good. LGTM. And we'd definitely would welcome a PR to update rejections to use an exception.

dvoytenko added a commit that referenced this pull request Oct 27, 2015
Simplify rejection handler to propagate error by re-throwing reason.
@dvoytenko dvoytenko merged commit cf197d7 into ampproject:master Oct 27, 2015
@jpettitt jpettitt mentioned this pull request Jun 10, 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.

4 participants