I have an asynchronous function a that selects a row's columns and then updates them after x amount of time, and an asynchronous function b that accesses these same values. I'm trying to make it so that function b waits for function a to be done with fetching and updating the row's values before it accesses it. I think the answer to this has something to do with Transactions but I'm not sure how to implement it.
Here's my attempt on it:
this.storage.Instance.transaction({
isolationLevel: Transaction.ISOLATION_LEVELS.SERIALIZABLE,
}, async transaction => {
this.storage.Guilds.findOne({
where: {
id: 1,
},
lock: transaction.LOCK.UPDATE,
}, {
transaction: transaction,
}).then(guild => {
setTimeout(() => {
guild.settings = 2;
guild.save();
}, 10000);
});
});
this.storage.Guilds.findOne({
where: {
id: 1,
},
}).then(guild => {
guild.settings = 3;
guild.save();
this.logger.debug(guild.settings);
});
This is just a test I made, in practical terms these functions would be in completely different scopes. The second SELECT FOR UPDATE query is not waiting for the first SELECT FOR UPDATE.