-
Notifications
You must be signed in to change notification settings - Fork 792
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
Proposal for an alternative to throw-catch error handling. #291
Comments
Can't you just easily wrap that in an extra function? let withResult = promise => new Promise(res => promise.then(result => res({ result }), err => res({ err })) So if your original promise fails, you get back { err: 'whatever error...' } else { result: 'great result...' }. let wR = yield withResult(getFileFromDisk('...')) \C
|
Didn't think about that :) Kinda brilliant. The only problem is that i'd need to keep that magic function in a separate module and keep a local convention, but otherwise, nice :) |
Sometimes a proper error handling is important. The traditional try-catch error handling has the following "usability" issues:
Now, what i propose is adding an additional method, say
co.withResult
orco.wr
whatever. Instead of returning resolved value from yielded promises or throwing an exception if a yielded promise rejects, it will return a wrapper object,Result
.A pseudo implementation for
Result
largely insipred by Result in rust and Optional in java:Returning that instead of the result directly allows us to rewrite our contrieved example as follows:
and even if the error handling is not important, the resulting code can still be concise:
What do you think? =]
The text was updated successfully, but these errors were encountered: