How to perform an async operation on exit
<p>I've been trying to perform an asynchronous operation before my process is terminated.</p>
<p>Saying 'terminated' I mean every possibility of termination:</p>
<ul>
<li><code>ctrl+c</code></li>
<li>Uncaught exception</li>
<li>Crashes</li>
<li>End of code</li>
<li>Anything..</li>
</ul>
<p>To my knowledge the <code>exit</code> event does that but for synchronous operations.</p>
<p>Reading the Nodejs docs i found the <a href="https://nodejs.org/api/process.html#process_event_beforeexit" rel="noreferrer"><code>beforeExit</code></a> event is for the async operations BUT :</p>
<blockquote>
<p><em>The 'beforeExit' event is not emitted for conditions causing explicit termination, such as calling <code>process.exit()</code> or uncaught exceptions.</em></p>
<p><em>The 'beforeExit' should not be used as an alternative to the 'exit' event unless the intention is to schedule additional work.</em></p>
</blockquote>
<p>Any suggestions?</p>