Execute promises when all resolved (as DB Transactions)

I have two promises that need to act as Transactions, if one of them fails, a "rollback" should occur.

For example: When I create a user I want to send a confirmation email, but what happens if there is an error on sending the email, the user will be created but will never get a confirmation.

await saveUser(user)
await sendEmail(user)

If I switch the order, what happens when something goes wrong on creating the user? It will get a confirmation.

await sendEmail(user)
await saveUser(user)

I thought a Promise.all will do the trick, but it doesn’t.

await Promise.all([saveUser(user), sendEmail(user)]);

Is there a way to execute promises as transactions on Javascript? Promises should execute if and only if both of them resolve.

140 thoughts on “Execute promises when all resolved (as DB Transactions)”

Leave a Comment