title: How to perform cleanup of individually allocated things cleanly?
body: <p>Given is the following function:</p>
  <pre class="lang-js prettyprint-override"><code>function createDir(): { path: string, x: number, y: number };
  </code></pre>
  <p>If the function is executed multiple times in a row we have this:</p>
  <pre class="lang-js prettyprint-override"><code>const { path: path0 } = createDir();
  /* ... do something here */
  const { path: path1 } = createDir();
  /* ... do something ELSE here */
  const { path: path2 } = createDir();
  /* ... do something completely else here */
  const { path: path3 } = createDir();
  /* ... and here is another code block */ 
  </code></pre>
  <p>What is the most TypeScriptish way to remove all directories at the end, even if one of the functions fails in between? It seems I have to use a <code>try/catch</code> here but how do I perform a cleanup in the <code>finally</code> block of a <code>try/catch</code>?</p>
  <p>This solution looks unnecessarily complicated to me:</p>
  <pre class="lang-js prettyprint-override"><code>let cleanup: string[] = [];
  try {
      const { path: path0 } = createDir();
      /* ... do something here */
      cleanup.push(path0);
      const { path: path1 } = createDir();
      /* ... do something ELSE here */
      cleanup.push(path1);
      const { path: path2 } = createDir();
      /* ... do something completely else here */
      cleanup.push(path2);
      const { path: path3 } = createDir();
      /* ... and here is another code block */
      cleanup.push(path3);
  } finally {
      for (const path of cleanup) {
          fs.rmSync(path, { recursive: true });
      }
  }
  </code></pre>