-
Notifications
You must be signed in to change notification settings - Fork 8
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
Promise error handling #1
Comments
Glad you like it!
If you wanted to handle it with a default earlier, you could do
Is that enough? Or can you think of some syntax that would make it clearer? |
Hm, according to the documentation [1] "If the Promise is rejected, the await expression throws the rejected value". We had discussion with colleagues recently, studied our codebase (in other language) that uses manual error propagation (Golang nil,err style) and decided that exceptions would make it cleaner for us, as most of the time we're just doing manual error propagation, and being manual makes it error prone (pun intended :) ). To match JS [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await |
Actually it does currently match js await behavior -- because you can only
|
Probably I'm not clear in my explanations/terminology. Let me try one more time :) An example taken from an article describing relation between promises and async/await: let hn = require('@datafire/hacker_news').create();
// Old code with promises:
Promise.resolve()
.then(_ => hn.getStories({storyType: 'top'}))
.then(storyIDs => hn.getItem({itemID: storyIDs[0]))
.then(topStory => console.log(topStory))
.catch(e => console.error(e))
// New code with async / await:
(async () => {
try {
let storyIDs = await hn.getStories({storyType: 'top'});
let topStory = await hn.getItem({itemID: storyIDs[0]});
console.log(topStory);
} catch (e) {
console.error(e);
}
})(); What I'm trying to stress is that Given that reason + this ppx does not have a notion of |
Ahhh yes now I understand.
would do the trick? |
Hm, what would be the semantics of this construct once de-sugared? Shouldn't catch just return rejected promise? Is it possible to annotate a function like this: let f = [@async] () => { 42 }; So that its body is being wrapped in a try/catch and changed to return a promise (which either resolves in case of no exceptions or rejects in case something was thrown down the lines)? |
|
I'm opening this issue for discussion on the promise error handling.
The
Promise
module does not include support for rejections so far:Yet the
NodeContinuation
module returnsJs.Result.t
based on what was passed to the callback. If we promisify such callback, we'll get promise resolution or rejection. I'm not sure if it's node specific or general JS pattern. Actuallythen
supports 2 callbacks and it's recommended to use second one in some articles:May be that should be the way for
Promise
module, so that it returnsJs.Return.t
based on what callback was invoked akin toNodeContinuation
? Probably reject type should beexn
.ES7
await
just seems to throw rejection as exception, future.js (bundled with node-fibers) is also following the same pattern.I've got a recommendation on the Discord to never reject promises unless it's fatal and just resolve them with
result
type of value. This works fine if you own all the code, but the benefit of using node ecosystem is leveraging large number of "batteries", and those do reject as a way of indicating errors. It makes sense to follow ecosystem conventions instead of fighting them and thus I'm thinking about a way to do that within reason_async.P.S.: I've tried out this cool project recently and I really like the way it sugared promise code looks, very clean! Thanks for your work!
The text was updated successfully, but these errors were encountered: