Resource management (eg. try-with-resources)

T.J. Crowder tj.crowder at
Fri Nov 10 15:32:28 UTC 2017

On Fri, Nov 10, 2017 at 3:00 PM, Michał Wadas <michalwadas at> wrote:
> After I started writing code  extensively using async-await I have
noticed that I would like to have automated way of dealing with resource

This is something I'd really like to see as well.

What's the motivation for having an `open`? Java's `AutoCloseable` just has
`close`; one would normally be acquiring these from a constructor or
function, which is effectively the `open`...?

Can you give a concrete example using the syntax you're looking at? E.g.,
were you really thinking of actually having `with` and `as`? If so, I'd
rather steal more directly from Java (perhaps replacing their `;` with `,`):

try (a = giveMeAResource(), b = giveMeAnotherResource()) {
    // `a` and `b` are in scope here
} catch (e) {
    // ...`a` and `b` are not in scope here
} finally {
    // ...`a` and `b` are not in scope here

...where (off-the-cuff):

* Both `catch` and `finally` are optional
* `a` and `b` are constants scoped to the `try` block (no need for the
keyword, but could require or allow it for clarity)
* Trailing commas on the resource declaration/initializer list are fine
* `b[Symbol.close]` is called first, then `a[Symbol.close]`
* Both `close`s are called prior to the execution of code in the `catch` or
`finally` (if any)
* If the `try` block throws, that error is the primary error and any
`close` calls that also throw are "suppressed errors" (more below)
* If the `try` block doesn't throw, but any `close` calls do, the *first*
`close` that fails becomes the primary error; any ohers that fail become
suppressed errors

Suppressed error handling:

* If the primary error is an `Error` object, suppressed errors are added to
a `suppressed` array property on it (creating it if necessary)
* If the primary error is not an `Error` object, they're lost other than
being treated as unhandled errors by the environment (for instance: logged,
perhaps causing script termination; thought required on that)

-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list