Cloud Function afterSave Aysnc/Await clarification

I am confused as to how Moralis will treat my code below on an afterSave.

I need to code to look for any previous winBetUser that match the query criteria and delete it, then save the new winBetUser.

The way I have the code below, I am feeling that perhaps the await winBetUser.destroy() and the winBetUser.save() are happening asynchronously, independently from one another.

const logger = Moralis.Cloud.getLogger()

const winBetUser = async (request) => {
  const confirmed = request.object.get('confirmed')
  if (confirmed) {
    const WinBetUser = Moralis.Object.extend('WinBetUser')

    // Clean out any previous WinBetUser from the table using the params
    const query = new Moralis.Query(WinBetUser)
    query.equalTo('chain_id', request.object.get('chain_id'))
    query.equalTo('wl_operator', request.object.get('wl_operator'))
    query.equalTo('meeting_key', request.object.get('meeting_key'))
    query.equalTo('race', request.object.get('runner'))
    query.equalTo('runner', request.object.get('runner'))
    query.equalTo('wallet', request.object.get('wallet'))
    let winBetUser = query.first()
    if (winBetUser) {
      await winBetUser.destroy()
    } else {
      logger.info(`No WinBetUser found`)
    }

    // And save the new one
    winBetUser = new WinBetUser()
    const keys = [
      'transaction_hash',
      'action',
      'address',
      'amount',
      'amount_decimal',
      'bet_key',
      'chain_id',
      'chain_id_decimal',
      'confirmed',
      'meeting_key',
      'payout',
      'payout_decimal',
      'race',
      'race_decimal',
      'runner',
      'runner_decimal',
      'wallet',
      'winner',
      'wl_operator',
      'wl_operator_decimal',
      'wl_operator_fee',
      'wl_operator_fee_decimal'
    ]
    for (const key of keys) {
      winBetUser.set(key, request.object.get(key))
    }
    logger.info('Saving winBetUser ...')
    await winBetUser.save()
  }
}
// If we add a new chain we need to add a new event watcher
Moralis.Cloud.afterSave('WinBetUserLocalhost', async (request) => { await winBetUser(request) })

I am feeling that perhaps the await winBetUser.destroy() and the winBetUser.save() are happening asynchronously, independently from one another.

How are you verifying this? You can add a log (logger.info) before each action to get a general idea of when each happens.

Yeh I am logging and it seems to run in order, but I am still seeing rows that should have ben deleted.

I have modified my function here, still its not clearing out the previous record before adding a new one.

const winBetUser = async (request) => {
  const confirmed = request.object.get('confirmed')
  if (confirmed) {
    const WinBetUser = Moralis.Object.extend('WinBetUser')

    // Clean out any previous WinBetUser from the table using the params
    const query = new Moralis.Query(WinBetUser)
    query.equalTo('chain_id', request.object.get('chain_id'))
    query.equalTo('wl_operator', request.object.get('wl_operator'))
    query.equalTo('meeting_key', request.object.get('meeting_key'))
    query.equalTo('race', request.object.get('runner'))
    query.equalTo('runner', request.object.get('runner'))
    query.equalTo('wallet', request.object.get('wallet'))
    let winBetUser = await query.first()
    if (winBetUser) {
      const destroy = await winBetUser.destroy({ useMasterKey: true })
      if (destroy) {
        logger.info('1. WinBetUser destroyed')
      } else {
        logger.info('1. WinBetUser NOT destroyed')
      }
    } else {
      logger.info('1. WinBetUser NOT found')
    }

    // And save the new one
    winBetUser = new WinBetUser()
    const keys = [
      'block_number',
      'transaction_hash',
      'action',
      'address',
      'amount',
      'amount_decimal',
      'chain_id',
      'chain_id_decimal',
      'confirmed',
      'meeting_key',
      'payout',
      'payout_decimal',
      'race',
      'race_decimal',
      'runner',
      'runner_decimal',
      'wallet',
      'winner',
      'wl_operator',
      'wl_operator_decimal',
      'wl_operator_fee',
      'wl_operator_fee_decimal'
    ]
    for (const key of keys) {
      winBetUser.set(key, request.object.get(key))
    }

    const save = await winBetUser.save({ useMasterKey: true })
    if (save) {
      logger.info('2. WinBetUser saved')
    } else {
      logger.info('2. WinBetUser NOT saved')
    }
  }
}
// If we add a new chain we need to add a new event watcher
Moralis.Cloud.afterSave('WinBetUserLocalhost', async (request) => { await winBetUser(request) })

Try to add more logging.
You can also check if the destroy worded after it was called with another query.

The syntax for .save is different for when using master key, is .save(null, …)

I am trying this approach, using a before save to clean up

Try to test portions of code separately.