17 pointsby healeycodes7 hours ago3 comments
  • throwaway815235 hours ago
    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.
    • Rohansi5 hours ago
      It's also necessary here because TypeScript has exceptions and you'd expect your `defer`s to execute even when an exception is thrown.
    • gtoweyan hour ago
      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.
      • anttiharjuan hour ago
        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.

        • Joker_vD37 minutes ago
          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.
      • cute_boi17 minutes ago
        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.

    • kaashif4 hours ago
      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.

      • mcintyre19944 hours ago
        • kaashif3 hours ago
          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.

          • throw-the-towelan hour ago
            Yes, but you also have this problem with `defer`.
      • Rohansi14 minutes ago
        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.

      • okigan2 hours ago
        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!

  • geostyx2 hours ago
    I built a library to do something similar: https://www.npmjs.com/package/yaplib

    defer is the thing I miss the most coming from Go

  • fg1373 hours ago
    Would call stack/source map reasonably work with this?