4 ms·
Interesting. I sort of did a similar pattern in some of my code (https://github.com/bhouston/threeify/ https://github.com/bhouston/threeify/) but just with exi
by bhouston 3y ago
Interesting. I sort of did a similar pattern in some of my code (https://github.com/bhouston/threeify/ https://github.com/bhouston/threeify/) but just with existing TS capabilities:
export function using<T extends { dispose(): void }>(
resource: T,
func: (resource: T) => void
) {
try {
func(resource);
} finally {
resource.dispose();
}
}
// usage
using(new Resource(), (resource) => {
// insert code that uses the resource
});