title: Node.js program does not run the `finally {}` block after `try {}` and exit with code 0
body: <p>Today I'm writing some code with RxJS in Node.js and got very wired result: The program run the <code>try{}</code> block but did not run the <code>finally{}</code> block at all, exit with code 0 right inside the <code>try{}</code> block.</p>
  
  <p>After making my code short, in order to reproduce this issue, the steps are:</p>
  
  <ol>
  <li>first create a <code>BehaviorSubject</code> named <code>subject</code></li>
  <li>call <code>subject.asObservable().share()</code> and get a Observable named <code>obs</code></li>
  <li>call <code>obs.subscribe()</code></li>
  <li>call <code>await obs.first().toPromise()</code></li>
  </ol>
  
  <p>Then, at last, the program acts a very wired behavior: exit the <code>try {}</code> block without running the code inside the following <code>finally {}</code> block.</p>
  
  <p>The TypeScript code to reproduce it is as the following: run it, then you will get a one line output: <code>1. BEFORE TRY</code>, which is not expected!</p>
  
  <pre><code>import {
    BehaviorSubject,
  }                     from 'rxjs/Rx'
  
  async function main() {
    const subject = new BehaviorSubject&lt;number&gt;(-1)
    const obs = subject.asObservable().share()
  
    /**
     * if comment the following line, this program will output right as the following three lines:
     *
     * 1. BEFORE TRY
     * 2. AFTER TRY
     * 3. FINALLY
     *
     * otherwise, the output will only be:
     *
     * 1. BEFORE TRY
     *
     * very wired!
     *
     */
    obs.subscribe()
  
    try {
      console.log('1. BEFORE TRY')
  
      await obs.first().toPromise()
  
      console.log('2. AFTER TRY')
    } finally {
      console.log('3. FINALLY')
    }
  
  }
  
  main()
  </code></pre>
  
  <p>My question is: <strong>How could the Node.js exit inside a <code>try{}</code> block without running the <code>finally{}</code> block, with the exit code 0?</strong></p>
  
  <p>Does there anyone have ideas?</p>
  
  <p>Env:</p>
  
  <pre><code>$ uname -a
  Darwin zixia-pro.lan 16.7.0 Darwin Kernel Version 16.7.0: Tue Jan 30 11:27:06 PST 2018; root:xnu-3789.73.11~1/RELEASE_X86_64 x86_64
  $ node --version
  v9.8.0
  $ grep rxjs ../package.json 
      "rxjs": "^5.5.6"
  </code></pre>
  
  <h1>UPDATE</h1>
  
  <p>With the answer from @martin, I realized that this is the behavior of Node.js.</p>
  
  <p>I write a reproducible program to demo how to exit the Node.js right in the <code>try{}</code> block and exit with code 0 without run the <code>finally{}</code> block, it's very simple: await a never resolve Promise.</p>
  
  <pre><code>async function main() {
    try {
      await new Promise(r =&gt; console.log('IN TRY BLOCK'))
    } finally {
      console.log('IN FINALLY BLOCK')
    }
  }
  
  main()
  </code></pre>
  
  <p>The above program will only output <code>IN TRY BLOCK</code> and exit with code 0, without output <code>IN FINALLY BLOCK</code> at all.</p>
  
  <p>For the readers who feels this is strange too, I had also opened a issue for node at <a href="https://github.com/nodejs/node/issues/19929" rel="nofollow noreferrer">https://github.com/nodejs/node/issues/19929</a></p>