Hook with non-awaited async functions #17263
-
Hello!! I have assigned an function to the The code is something like:
Can this transaction be committed before
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
That is not something we can guarantee, you should await them before the end of the transaction: const nonAwaitedTransactionQueries = new WeakMap();
User.beforeUpdate(applyChanges);
function applyChanges (user, { transaction } = {}) {
const promise = foo(user, { transaction });
if (transaction) {
if (!nonAwaitedTransactionQueries.has(transaction)) {
nonAwaitedTransactionQueries.set(transaction, [])
}
nonAwaitedTransactionQueries.get(transaction).push(promise);
}
}
// in the transaction code
await sequelize.transaction((transaction) => {
await user.save({ transaction });
const promises = nonAwaitedTransactionQueries.get(transaction);
await Promise.all(promises);
}) |
Beta Was this translation helpful? Give feedback.
-
@ephys, thanks for taking the time to answer my question. Is this solution you suggested would have the same result as assigning an async function to the hook and actually awaiting all the functions that can be awaited? If not, how do they differ? |
Beta Was this translation helpful? Give feedback.
That is not something we can guarantee, you should await them before the end of the transaction: