title: How to get node to exit when mongo connect fails
body: <p>I'd like my Node JS app to exit immediately if it can't connect to Mongo. I'm using the <code>mongodb</code> node library.</p>
  
  <p>I've reduced the code down to</p>
  
  <pre><code>const {MongoClient} = require('mongodb');
  MongoClient.connect('mongodb://localhost:27017');
  </code></pre>
  
  <p>If Mongo is <strong>not</strong> running, I get an <code>UnhandledPromiseRejectionWarning</code> with <code>ECONNREFUSED</code>, which I fully expect, but then the program hangs and never exits. This is with Node version 10.0.0.</p>
  
  <p>Since the connection never succeeded I don't have a connection handle to close. I've tried various ways to <code>catch</code> the rejected promise, but I have been unsuccessful in getting the program to exit.</p>
  
  <p>What do I need to do to shut down the MongoClient and make the program exit in this case?</p>