New Keyword "using" in JavaScript!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
typescript 5.2 new keyword using which is also apparently as I've been told reliably by chat the least degenerate group of people in the universe that this actually is a JavaScript feature it's in stage four it's using Sparkles using uh there we go typescript 5.2 will introduce a new keyword using that you can use to dispose of anything with the symbol dot dispose function when it leaves scope which is kind of interesting um right away I started thinking this is like Drop light from rust and I wonder what it means by leave scope are they actually welcome Costco thanks for leaving alerts on okay I left alerts on I left alerts I left alert effectively it's like you get a Destructor to be called when something leaves scope I'm very curious what they mean by this because it's kind of crazy to think that we would ever want this in JavaScript I'm gonna be real here I'm extremely skeptical that this is a good idea we'll see we'll see uh this is based on tc39 proposal which recently reached stage three indicating that it's coming to JavaScript using will be extremely useful for managing resources like file handles database connections and more yeah I figured this would be the use case right because this is just destructors destructors available in JavaScript we'll see uh let's see is a new Global symbol in JavaScript anything with the function assigned a system.dispose will be considered a resource an object with a specific Lifetime and can be used with the using keyword okay a weight using you can also use async dispose and the weight using oh this beautiful to handle uh resources which need to be disposed asynchronously get resource async some await function beautiful await using resource this seems exciting uh this will await the symbol of sync async dispose okay so actually I do have a little bit of a hard time here what what exactly goes on in this code I'm having a hard time kind of walking through this in my head so when I see the term await I assume that this will stay until whatever is happening is finished get resource is not async right it's a function that returns something I'm having a hard time trying to understand this meaning that when does this thing when does using get kicked on using must get kicked on at the end of the scope but why is the await word here which makes it hard because I assume you'd go await using resource equals resource then you do some code then at the end of this little uh at the end of this scope then it would dispose it which would also be awaited am I correct on that the problem is is this example is just not big enough for me to understand no no text Heavy that makes less sense because you don't need to wait that yeah the dispose is automatic no I trust okay I understand how get resource works if you look at the code it's right here it's a function see that first off this is why I hate I I despise that this is like current JavaScript Style this this it feels like the most try hard annoying feature of all time why don't people just do this I don't understand it why are you just not making it clear I don't I just I don't I don't get it right like I just don't understand why people have to do that I don't get it I I just I mean sure why don't you just make this a reality in JavaScript okay make FN a function if you want to it just makes no effing sense that you do this the amount of symbols and the fact that you have to wrap this thing with the parenthesis to tell you it's actually returning this thing I like Arrow functions but they have meaning okay they have actual meaning second off every single yes a class would be better here because every single time you call this function you actually generate a new object plus you generate a new function right you're generating new items this isn't nearly as optimized as a class can be because classes are obvious what you're attempting to optimize right whereas this isn't uh oh I have always hated this pattern so right now I don't understand I don't understand the execution order because here in my head here let me go like this uh here whoopsies uh like don't get me wrong I do love Arrow functions when they're used properly okay so I see this my question is uh can I do things here then does a resource uh get disposed here there we go right so what it looks something like this then I'd go something like a resource dot uh you know do something right oh thank you thank you co-pilot and then I could await that right which actually what's happening here okay so this is what's actually happening here then which code I I don't they should have used the term defer something so what's actually happening here is this const resource equals get resource a way to do something await uh resource dot async disposal that's what's happening and there could be more they're actually well you're right there actually could be more there actually could be a hidden try catch somewhere in here that we're missing as well right they're they're very well could be a a hidden try catch that's going on as well um it disposed in the correct order yes and disposed in the correct order but I I just this line sucks and what I mean by this line sucks is that this await doesn't happen where you think it does what I would much rather see in something is something like deferred uh resource dot uh dispose right and then if your dispose is async you can await that right this on the other hand is very clear what's going to happen right you know for a fact that this gets executed at the end of the item whereas this await is actually in a weight further down the chain I feel yeah if you went like this that means this function would have to be uh async right oopsies right that's what that means I just don't like the lot I mean yes it won't it's not hard to understand what's happening I just like better words here me personally better words here okay I like better words that's a me personally kind of thing uh I don't I don't like what I see I don't like what I see because I don't like it right I would rather honestly I'd rather because I don't like that a weight doesn't have an effect here but later on it looks confusing in my head what I had honestly what I'd rather see is something like this using that's a function that you have to do like get resource and then yeah then you have this whole then you have this and then it manages it at the end right it does and then you can await that because now I can read this from left to right if we're not doing defer I can read this from leftover left to right I can I can tell right away that I will await the end of this thing and we'll get the resource and then we'll do our async stuff and when it's done with that function it will actually await the results and await the disposal to me this makes 10 times more sense than having you know I can't you have to you just have to have that knowledge I don't know I don't like I'm not sure if I like okay me personally okay it's a me personal thing I understand you may love it uh anyways let's see uh yeah okay use yeah file handles yep these are great you always want to do this right this is just like such a great thing to do uh people forget how useful this is and sometimes you forget to close your file handle which is also a little bit surprising I just assumed that the moment this thing is g-seed it also gets closed but you probably don't want it open that long right I I understand why you want this in the sense that you don't want to have to wait for GC to close your file handle you would much rather have your file handle close right away correct so instead you'd have something like this right I really am not loving this pattern um yeah I don't really love the pattern I don't love the pattern but I get it GC doesn't close the file handle when the really so it's just permanently opened huh interesting okay yeah okay interesting uh that's why you should just always close your files huh huh huh uh database connections yeah this also again this makes sense I understand why people want this uh not I'm not arguing that I like the idea of defer I'm arguing that this is a strange way to say defer right this is just a strange way to say defer not sure if I love it this is also I mean this is really kind of crazy syntax as well that you're actually lifting out the value of connection while having the dispose be a part of some outer thing it's I mean it's pretty wide it's pretty wild the idea is nice I like it I don't mind it thank you Matt for uh throwing this I like how Matt has a signature on his website look at that Matt has his signature on his website I don't have a signature on a website JS getting stranger I'm not sure if JS is uh that great but I do like that he has a async function main look at that he has a main function in JavaScript look at it go look at it go uh huh yeah I don't like the look of it but whatever I'll just get used to it you know I'm gonna use it I'm gonna use it you know I'm gonna you know I'm gonna be using it right I'm gonna use it it's gonna be great I'll be happy that I have it um I'm just not stoked about it you know what I mean I'm just not like I I'm just not in love with it you know the disposed symbol is defined by libraries yeah I will defer judgment on it I will I will be async using judgment on it it's extremely useful absolutely because then you don't have to do try catch I am oopsies hey unless hey unnecessary dick riding buddy with your extremely useful okay it's nice I'll give it to you I'll give that to you okay it's nice but that's all I'm giving to you okay uh they're they're saving the defer keyword for the unavoidable unfortunate future where they introduce the fur and the and they make a typo oh they oh D ref would make it yeah uh let's see okay what about Dick's riding use using beautiful it looks like resources don't compose it doesn't well yes it really looks like well in this example all resources are like inlined which probably isn't a great way to go about things I don't think it's great I wonder if classes are more performant than just objects like this I've never actually done any sort of testing to find out the differences but it'd be kind of interesting to see what is the consequence of programming like this versus the consequence of programming differently um they should be yes it typically that's how I would think because you're signaling to the compiler that this shape is like well first off you don't have to ever recreate a function right you're recreating functions here you only have one function ever created so you would assume that it's better but you know it's JavaScript you can't you can't it's not that minor there's some crazy things that will destroy there's crazy things that will destroy your performance in JavaScript it's nuts it's crazy it's crazy uh it's similar to the eye disposable yeah it is but still still kind of hard I want to avoid the monkey and banana problem okay I'm not sure what that means but I appreciate you for saying that you know what I mean hey guess what the name is the disposal jet
Info
Channel: ThePrimeTime
Views: 59,083
Rating: undefined out of 5
Keywords: programming, computer, software, software engineer, software engineering, program, development, developing, developer, developers, web design, web developer, web development, programmer humor, humor, memes, software memes, engineer, engineering, Regex, regexs, regexes, netflix, vscode, vscode engineer, vscode plugins, Lenovo, customer service
Id: UN5PJh4RxFs
Channel Id: undefined
Length: 11min 29sec (689 seconds)
Published: Sun Jul 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.