Skip to content

Instantly share code, notes, and snippets.

@stephenplusplus
Last active March 28, 2017 20:27
Show Gist options
  • Save stephenplusplus/3cb49529ee537c640a4670edf900e294 to your computer and use it in GitHub Desktop.
Save stephenplusplus/3cb49529ee537c640a4670edf900e294 to your computer and use it in GitHub Desktop.
How to handle a chain of promises that need to be retried. Reference discussion: https://github.com/GoogleCloudPlatform/google-cloud-node/pull/2144#discussion_r108394699
// A)
datastore
.runTransaction(function(err, transaction) {
if (err) {
// Could not start transaction
return Promise.reject(err)
}
// ...transaction stuff...
return transaction.commit()
})
.then(success)
.then(doSomethingElse)
.catch(handleErrors)
// B)
datastore.runTransaction(function(err, tranaction) {
if (err) {
// Could not start transaction
}
// ...transaction stuff...
transaction.commit()
.then(success)
.then(doSomethingElse)
.catch(handleErrors)
})
@stephenplusplus
Copy link
Author

stephenplusplus commented Mar 28, 2017

This is for our Cloud Spanner API-- as part of the nature of a Transaction, they can fail rather commonly. The best handling for this (as advised by the API team) is retrying everything. So that means retrying all arbitrary user code up until they are ready to commit the transaction.

To handle this, we have to encapsulate all of that code so that we can easily retry it. That forces us to create a sandbox of sorts, inside of datastore.runTransaction(). We are deciding between two approaches, judging them solely on the basis of what is most intuitive and less prone for error or misconception, given the nature of the situation.

Any thoughts or completely alternative solutions would be very appreciated.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment