4 ms·
I tried to write a `using` utility for JS a few years ago: https://gist.github.com/davidmurdoch/dc37781b0200a2892577363f30b6c545 https://gist.github.com/davidmu
by davidmurdoch 1y ago
I tried to write a `using` utility for JS a few years ago: https://gist.github.com/davidmurdoch/dc37781b0200a2892577363f30b6c545 https://gist.github.com/davidmurdoch/dc37781b0200a2892577363...
It's not very ergonomic so I never tried to use it anywhere.
- masklinn 1y agoThe error was probably trying to write a generic `using`. In my experience languages which use higher order functions or macros for scope cleanup tend to build high-level utilities directly onto the lowest level features, it can be a bit repetitive but usually not too bad. So in this case, rather than a generic `using` built on the even more generic `try/except` you should probably have built a `withFile` callback. It's a bit more repetitive, but because you know exactly what you're working with it's a lot less error prone, and you don't need to hope there's a ready made protocol. It also provides the opportunity of upgrading the entire thing e.g. because `withFile` would be specialised for file interaction it would be able to wrap all file operations as promise-based methods instead of having to mix promises and legacy callbacks.
- davidmurdoch 1y agoSure, the with* pattern is fine. I was just playing with the idea of C#s disposable pattern in JS.