-
Notifications
You must be signed in to change notification settings - Fork 30
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
Possible to tell whether an error occurred in the body or not? #234
Comments
The way I would normally model this is by having a method or property that indicates the transaction completed successfully: async function addPost() {
await using transaction = await beginTransaction();
await transaction.createNewPost('hello world');
transaction.setComplete();
// or,
// transaction.succeeded = true;
} Marking a transaction as successful indicates whether to commit or rollback when the transaction is disposed. If you never reach the success line, the transaction performs auto-rollback. This is similar to how Note that this does differ from |
I see, I'll explore the approach you suggested! So, I take it my original usage is not possible with this feature, where the |
IMO dispose ought to receive the error. |
For example, this proposal is currently unsuitable for managing a database transaction. The usual pattern (this example comes from pg-promise): await db.tx(async transaction => {
// transaction body
}); If an error is thrown during the transaction - a database error or otherwise -- the transaction is rolled back. Otherwise it is committed. Transactions are a canonical use case of resource management. It's disappointing this common pattern cannot actually leverage this proposal as is. I recommend revisiting that. The obvious solution is to pass the error as an argument. {
[Symbol.asyncDispose]: (error) => {}
} |
Agreed! And it just doesn't get passed if there is none. I assume this proposal is too hardened to change this now? |
The design you referenced is designed in this way since it can leverage a callback. It is not necessarily an implicit behavior of a transaction itself. In languages like C#, you use Languages like Python allow you to observe an exception, but also allow you to act on the exception, including possibly suppressing it. While we do have #49 for discussion of this capability, there are some on the committee (myself included) that are uncomfortable with the complexity that might add. As a result, there is a line that must be drawn between what we do and don't want I'd much rather defer passing an exception to something like #49, which we can revisit in a future proposal if necessary. |
I'm closing this issue. For further discussion on this topic I would suggest you refer to #49. |
Thanks for the pointers, @rbuckton! |
Not sure how to better name this issue and if the answer is already contained in this repo - I wasn't able to find anything.
I am interested to know if I can use
using
to model a database transaction API, like this:I'd like to obtain this behavior:
dispose
call oftransaction
should commit the changes to the database.dispose
call oftransaction
should roll back the changes.Would be awesome if this was possible as it removes a level of indentation that I currently use to model this. However, for this to work, the
dispose
function would somehow need to know if it's being called after a successful run or due to an early exit caused by an error. Is this possible?If it's not possible, the next best thing I could think of is:
Then, when disposing, the transaction can check if it has been committed. If not, the
commit
statement has not been reached, implying an error occurred (or implying the programmer just forgot to callcommit
, which is an error I'd like to make impossible).But this approach is suboptimal, especially when the function has more than one exit point, such as early returns. The programmer would need to remember to commit at each exit point.
The text was updated successfully, but these errors were encountered: