-
Notifications
You must be signed in to change notification settings - Fork 70
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
prommiseResult throws an error #208
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Didn't think of that, the throwing-Error or return the success value just beautifully fit!
@ailisp @volovyks I would still argue in favour of having a {status, result}, as JS promises do, since they are simpler to explain to developers. Lets compare a simple workflow, where the callback returns the result value: result structurelet response = near.promiseResult();
if(response.status === success){
log("it worked")
return response.value
}else{
log("it didn't work because:", response.error)
return
} try/catchtry{
let result = near.promiseResult(); // if this fails the catch arises
log("it worked")
return result
}catch(e){
log("it didn't work because:", e.message)
return
} I personally find the first logic simpler to read and explain for devs. |
@ailisp @volovyks we could even use the status |
I think that the concept of Let's not forget that return near.promiseResult() // bad practice, use try-catch! Looks like we need 1-2 more opinions in order to make a decision. @BenKurrek @doriancrutcher @petarvujovic98 |
I vote this PR's throw behavior. Even if the developer forget to write try-catch, the throw will cause contract panic and there's not going to be a security issue. Also, throwing an error is more common practice in JavaScript libraries than return |
Ok, merging. We can always come back to this question if developers start complaining. |
@ailisp @volovyks I was thinking yesterday, and there is another thing I don't like about the try/catch pattern: try{
let result = near.promiseResult(); // if this fails the catch arises
// but if I have a bug/error here also!
} If I introduced a bug in my callback method (for example, accessing the wrong index), it will be masked by the try/catch. This can be even dangerous in the classic situation where:
If my callback has an error, the code will act as if the xcc failed and send money back to the user. The solution to that is to isolate the try/catch, and check later if the promise succeeded: let result: type = undefined;
let xcc_succeeded: bool = false;
try{
let result = near.promiseResult(); // if this fails the catch arises
xcc_succeeded = true;
} catch(e){ }
if (succeeded){
// it succeeded: do something
}else{
// it failed: rollback
} In that way, we:
But this is a lot of boilerplate. Because of this, I think the |
Maybe having both options can cover all cases |
I think that the I also think that the |
We should not mimic the Rust design here. Throwing an error is the standard behavior for JS.
The same goes for the return type. I do not want to add {error, result} because it would be the only place in the whole API where we use such a design.