I have a graph editor, where user has option to create a node. It gets connected with all currently selected nodes. In google document, it looks like a node (its string label) is mapped to the comma-separated set of connected labels. So, to add a node, I first create an empty map item
map.set(name, "");
and, then, separately add the connected items
if (map.get(a) == null) throw new Error("node " + a + " does not exist") // fails here
if (map.get(b) == null) throw new Error("node " + b + " does not exist")
map.set(a, a_connections)
map.set(b, b_connections)
The problem is that map.get detects that node is not added into the map yet. It takes some time. It seems that the operations are non-blocking even within single JS client (read-my-writes inconsistent). How am I supposed to work with that?
I have noticed this inconsistency when tried to establish two connections (just to detect when connections are failed because it can happen that connection is lost and all my edits do not propagate to server and I wanted to the user to know about that).
This page has some details on conflict resolution and things you can do in order to have your changes be applied together.
I'm a little confused from your example what the problem/expected behavior is.
If you do map.set("foo", "") followed by map.get("foo") on the same client from within the same synchronous block the get will always return what you set.
If you do it from different synchronous blocks, but on the same client, get will return something different only if another client made a change to the value of "foo".
If you are doing the set and get on different clients, then the value of "foo" can take an arbitrary amount of time to propagate to the other client. You should be able to register a listener to discover when it is set.
If you'd like to track when all changes have been persisted, you can listen to DocumentSaveStateChangedEvents