Created
December 14, 2020 14:13
-
-
Save benjamingr/43014ac85ac9f0e67b6a66227fa9a30e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's say I have transaction code that looks like: | |
let state = 1; | |
session.withTransaction(async () => { | |
// this can retry, which means otherFn can run twice and possible insert the wrong value or in the wrong order | |
state++; | |
await Promise.all([ | |
coll1.insertOne({ abc: 1 }, { session }); | |
otherFn(state, session), | |
]) | |
}); | |
async function otherFn(state, session) { | |
// get some data from redis or another DB | |
await setTimeout(Math.random() * 100); | |
await coll2.insertOne({abc: state}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment