Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why didn't they go with `defer` which is the more ergonomic and established pattern?

`defer` also doesn't need to change the API of thousands of objects to use it, instead now you have to add a method any resource like object, or for things that are not objects, you can't even use this feature.



> Why didn't they go with `defer` which is the more ergonomic and established pattern?

Neither statement is true.

> `defer` also doesn't need to change the API of thousands of objects to use it

Callbacks can trivially be bridged to `using`:

    using _cb = {[Symbol.dispose]: yourCallbackHere};
There is also built-in support for cleanup callbacks via the proposal’s DisposableStack object.

> or for things that are not objects

This is javascript. Everything of note is an object. And again, callbacks can trivially be bridged to using


For readability I'd recommend using DisposableStack instead of making an object, though either works:

    using disposer = new DisposableStack;
    disposer.defer(yourCallbackHere);


I can't think of any JS value that could need cleaning up that isn't an object.


what about functions that return a callback for disposal? like Redux store.subscribe, there are thousands more examples




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: