Async Writes to a Dict?
Hi there,
I just started working with Node in Max 8–thank you Cycling team for your work here, I love the feature!
I've been experimenting with the API and I was having trouble doing async writes to a Dict–basically, it looks like the last write wins. I just wanted check to see whether this is expected behavior or I'm doing something wrong. Here's some example code (in this case, I have iterable query params from a webserver):
const updateOperations = Object.keys(params).map((key) => {
return Max.updateDict("testing", key, params[key]);
});
await Promise.all(updateOperations);
When I run the code above, I'm seeing the last-write-wins behavior. However, if I put the await inside a loop and force individual writes to block, they all show up in the Dict:
Object.keys(params).forEach((key) => {
await Max.updateDict("testing", key, params[key]);
});
Again, I just want to verify that this is expected–I don't need concurrent writes, but I'd hate to miss out if they're possible. :)
Thanks for all the great work and congratulations on the release!
cheers,
hdh
Hey deanh.
The difference between the Promise.all based approach and the async/await is the fact that the former actually runs in parallel. As of now Max.updateDict is not really handling these parallel calls as atomic as you might expect.
So yeah, if u need everything to happen at the same time I'd suggest using a combo of Max.getDict and Max.setDict and a JS based solution on updating the contents of the dict before writing it again. Other than that you might be stuck with the for-loop and sequenced writes approach.
Thanks!
Florian
Got it. Thank you for the follow up!