It's another extension, the enhancement that recently got added into proposal on stage one is having an Async match. You can have an Async match with an await matchable and, and do the same thing, how we will do it with, on a non-Async way of matching, you could say when A you can await and when B you could do B.then. So basically it produces a promise and you could do an Async match with pattern matching as well.
The other interesting feature of what we have on stage one is do expression. So the do expression basically helps you to, you know, have a block within do, where the last statement is written by default. Say you have in this example X, where, where you say temp is a function call and you say temp is temp into Templus1, that's what the default value is written. But how is it really useful? Say, if you have multiple branching in your code and how would you do it today? Suppose you have constant let or a combination of that in your code, or you want to wrap it within a function to, you know, to maintain the scope and things like that. Here's an example of multiple branching. You could do it as easily as saying, Hey, here's my do expression and I have my if condition. If foo is matching, then I would invoke the function f and that's written value will be assigned to X here else. If it's bar, then you would, you would go ahead and invoke G and that the value of the invocation of G would be assigned to X here again or else you would call it. So you could, you could see how intuitive it looks like compared to probably you'd have done this with the ternary operator and have nested conditions within which kind of makes it, makes your code a bit ugly and probably even debugging tougher times.
Here's another easier, you know, syntax for jsx. This probably most of our friends who are into react would have faced, say if you want to conditionally render components. You would probably put a ternary operator on it, or probably use another component which conditionally evaluates the logic required and renders the required component, right? So it kind of gets messier. Here's a view. Here's how beautifully it looks with do expression. You say you have a nav, you have the home component and you say do if logged in, show the logged out button, else show the login button component. So it's so easy to express that here with the do expression.
Of course, do expression doesn't just stop there. Let's see another interesting proposal. So as of today, if you're using promise.all which has await within it, you would probably wrap it with async functions, immediately invoke async functions, because you can't really use await in the top level unless until you're in MJS, right? So until unless you are in a module, basically. So if you use it today, you do promise.all and wrap each of them with async functions, and then you would go ahead and fetch the results and operate on it. So async do on stage one is exactly this proposal, which is more like an extension of the do expression, but on the async level, which makes things even more easier instead of wrapping them with an immediately invoked async function expression, you could just say async do and then pass in your own statements of fetching or any of it, or the, the logic required for that async block. So you can do an async do and this makes, you know, the code much more readable and you don't want to crack your head and wrapping it. Again, with an, you know, immediately invoked, async function. So this is on stage one, async do expression. And operating overloading on stage one. There's this big debate that does JavaScript really need operating overloading, does it really help. And this proposal is trying to explore that.
Comments