From the course: TypeScript Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Resource management with the using keyword

Resource management with the using keyword - TypeScript Tutorial

From the course: TypeScript Essential Training

Resource management with the using keyword

- [Instructor] In this video, I'll show you how to implement the using statement to ensure that the resources your application is consuming are properly cleaned up after use even when the code using them results in an error. Before I show this feature in action, I'll start by explaining how this code that I'm showing could benefit from it. This temp data class creates an abstraction around writing to a temporary file. It generates a file name for the data to be stored at, a right method that allows data to be written to the file, and a clear method that is expected to be called when the temp data is no longer needed. Then finally, we've got a function that creates an instance of this class and uses both of those methods. Now here's the problem. What if I forget to call the clear method when I'm done, or what if my application throws an error before I get to call the clear method? In this case, my temp file is left taking up disc space or in some other scenario, code like this could…

Contents