8/2/2026 at 9:38:31 PM
Article describes try/finally as a hack to get the effect of defer, but it looks to me like it's the other way around? Try/finally is more traditional, in one form or another.by throwaway81523
8/2/2026 at 10:10:49 PM
It's also necessary here because TypeScript has exceptions and you'd expect your `defer`s to execute even when an exception is thrown.by Rohansi
8/2/2026 at 10:36:15 PM
If the idea of computers is that they remember stuff for you and do stuff for you, then both try/finally and defer seem like hacks to work around not having RAII like e.g. Rust or C++ where resources are closed/disposed of/deallocated automatically.Try X finally dispose of all resources. X, defer clean up X. Or how about just X and cleanup is done automatically, with the author of the resources deciding what is needed to clean X up.
by kaashif
8/2/2026 at 10:50:09 PM
JS has the using keyword for this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...by mcintyre1994
8/2/2026 at 11:36:31 PM
What if you forget to use that and just use let or const? It is better than plain try-finally or defer because you don't have to remember how to dispose of the resources but in terms of remember to dispose of the resource at all, I don't think that is all that different from Python's with or Java's try-with-resources. You can just forget to use using, with, try-with-resources.There isn't any way to forget to drop a resource if you have RAII.
by kaashif
8/3/2026 at 1:50:24 AM
Yes, but you also have this problem with `defer`.by throw-the-towel
8/3/2026 at 4:53:51 AM
And try/finally.by masklinn
8/3/2026 at 5:49:12 AM
Only when doing stack allocations or reference counting, any other heap management strategy and RAII can't help as well.For example, combining RAII with lock free data structures, which usually ends up in techniques like hazardous pointers instead.
by pjmlp
8/3/2026 at 1:14:38 AM
it's mesmerizing that the fantastic ergonomics of RAII have not propagated to other languages.It's way easier to use, much clear, less typing, more predictable runtime behavior.
RAII... terrible name, great concept!
by okigan
8/3/2026 at 4:59:21 AM
RAII has complications in GC’d, fuzzy ownership langages, it becomes much less ergonomic there because now passing an RAII object as parameter to a function causes that function to clean it up, so you need additional mechanisms to bypass this. Same if you just poke an RAII object inside a collection.Traditionally langages with simpler runtimes simply used destructors for this, refcounting made it deterministic (modulo the old reference leak), but more advanced garbage collection schemes made that stop working.
by masklinn
8/3/2026 at 5:46:16 AM
It only works for stack allocations or when using reference counting, which isn't the ultimate performance of GC algorithms.by pjmlp
8/3/2026 at 2:52:42 AM
I mean, sure, but RAII (in C++, at least) is implemented the same way as this article: with a try...finally block!RAII doesn't really fit into every language because they don't all have deterministic destructors/finalizers and objects with scoped lifetimes. Sometimes you only have one but not the other and you definitely need both.
by Rohansi
8/3/2026 at 7:24:14 AM
that's a very broken metaphor - it might be "similar" to try-finally block if you only have one variable/object following RAII. But try-finally block(s) become complete mess when multiple variables are involved, and again requires way more typing/duplication than RIAA. And "similar" in terms of results, NOT similar in terms of performance as try {} block requires additional instructions to set it up, whereas RIAA is runtime free.And regarding sibling post about finalizers -- another broken concept as you cant use finalizers with limited resources (ex. graphic contexts, db connections, handles, locks, etc) and then these languages that use finalizers become complete mess if you need to manage such resources.
by okigan
8/3/2026 at 2:53:36 PM
> And "similar" in terms of results, NOT similar in terms of performance as try {} block requires additional instructions to set it up, whereas RIAA is runtime free.By similar I meant from the compiler's point of view. `try...finally` is the low level primitive to ensure some code runs at exit. If something throws an exception then destructors must run when unwinding the stack, which is what `finally` is for! But if you disable exceptions then `try` blocks are probably all basically no-ops.
by Rohansi
8/3/2026 at 4:11:27 AM
A (lexically-scoped) defer is a more general than a finally block. You can express finally-block semantics using defer.It's also more exception-safe when you have more than one throwing call in the try block.
by wannabe44
8/3/2026 at 2:12:57 AM
Finally isn't bullet proof. If you execute a promise in a try block and it happens to end without resolving, the finally block will be never be executed. Fun stuff.by gtowey
8/3/2026 at 2:21:33 AM
defer isn't bullet proof either. I think there's a linter about it and os.Exit.One can just wrap os.Exit in a helper to get the expected behaviour.
by anttiharju
8/3/2026 at 6:48:41 PM
I would expect to have a method that explicitly stops program execution immediately without further side effects.An unresolved promise that causes other code to be skipped while program execution continues after the block is, well, unintuitive to say the least.
by gtowey
8/3/2026 at 2:29:13 AM
Also, if you pull the power plug, neither finally blocks nor defers would run. I personally consider it a clear and obvious deficiency in the semantics (and the implementations) of those programming languages but everybody refuses to listen to me.by Joker_vD
8/3/2026 at 6:52:50 PM
Back in the days of spinning rust, we had RAID controller cards with batteries, so that if someone pulled the plug, there would be enough power to keep the drives going until any buffered writes were completed.I certainly wouldn't mind if modern servers had something similar to make sure programs could stop gracefully. But then that doesn't help if you have a hardware failure...
by gtowey
8/3/2026 at 2:48:48 AM
I think this is correct behavior.``` async function run() { try { await new Promise(() => { // The executor function finishes, // but it never calls resolve() or reject(). }); } finally { console.log("cleanup"); } } ```
I never expect cleanup to be logged.
by cute_boi
8/3/2026 at 5:21:00 AM
Author here. I find try/finally verbose and vulnerable to mistakes. For me, it adds "function noise".by healeycodes