Skip to content

Hook with non-awaited async functions #17263

Discussion options

You must be logged in to vote

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(p…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by miguelvieiraramos
Comment options

You must be logged in to vote
1 reply
@ephys
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants