title: Mongoose pass data out of withTransaction helper
body: <h2>Introduction</h2>
  
  <p>Hey there,</p>
  
  <p>I am trying to pass out data from the mongoose <code>withTransaction</code> callback. Right now, I am using the following code which implements callbacks:</p>
  
  <pre><code>const transactionSession = await mongoose.startSession()
  await transactionSession.withTransaction(async (tSession) =&gt; {
      try {
          // MARK Transaction writes &amp; reads removed for brevity
  
          console.log("Successfully performed transaction!")
          cb(null, "Any test data")
  
          return Promise.resolve()
      } catch (error) {
          console.log("Transaction aborted due to error:", error)
          cb(error)
  
          return Promise.reject()
      }
  })
  
  } catch (error) {
      console.log(error)
      return cb(error)
  }
  </code></pre>
  
  <p>A more detailed snippet of the <code>withTransaction</code> helper in use can be found <a href="https://gist.github.com/linus-hologram/0d33e55e079486967f9f4a13c3d26383" rel="nofollow noreferrer">here</a>.</p>
  
  <p>A link to the official Mongoose documentation regarding the <code>withTransaction</code> helper can be found <a href="https://mongoosejs.com/docs/transactions.html" rel="nofollow noreferrer">here</a>.</p>
  
  <p><strong>At the moment, I am using a callback to pass out data from the <code>withTransaction</code>callback:</strong></p>
  
  <p><code>cb(null, "Any test data")</code></p>
  
  <p>However, the problem is that naturally the callback is executed first, before the <code>Promise.resolve()</code> is returned. This means, that (in my case) a success response is sent back to the client before any necessary database writes are committed:</p>
  
  <pre><code>// this is executed first - the callback will send back a response to the client
  cb(null, "Any test data")
  
  // only now, after the response already got sent to the client, the transaction is committed.
  return Promise.resolve()
  </code></pre>
  
  <p><strong>Why I think this is a problem:</strong></p>
  
  <p>Honestly, I am not sure. It just doesn't feel right to send back a success-response to the client, if there hasn't been any database write at that time. Does anybody know the appropriate way to deal with this specific use-case?</p>
  
  <p>I thought about passing data out of the <code>withTransaction</code> helper using something like this:</p>
  
  <pre><code>const transactionResult = await transactionSession.withTransaction({...})
  </code></pre>
  
  <p>I've tried it, and the response is a <code>CommandResult</code> of MongoDB, which does not include any of the data I included in the resolved promise.</p>
  
  <h2>Summary</h2>
  
  <p>Is it a problem, if a success response is sent back to the client before the transaction is committed? If so, what is the appropriate way to pass out data from the <code>withTransaction</code> helper and thereby committing the transaction <em>before</em> sending back a response?</p>
  
  <p>I would be thankful for any advice I get.</p>