Live data from Hacker News

New Features in ES2019

javascript.christmas

121–123 of 123 posts

Re: New Features in ES2019

#121
post #118

Earlier quoted context omitted.

Opportunistically loading from a cache

But if loading data from the cache fails, wouldn't you have to do something else to get the data? Or am I misunderstanding?

Only if you really need the data. Maybe it's nice-to-have.

Re: New Features in ES2019

#122
post #78

Earlier quoted context omitted.

I expect it's because of `finally`. You can write a try without a catch today, provided there's a finally, however the error will continue to propagate in that case. try {throw new Error('')} // exception is caught try {throw new Error('')} // uncaught exception finally {console.log('finally')} That would be a bit of a footgun, as adding a finally clause that does something innocuous like logging would result in an u…

That makes sense. I guess `try` means something a bit different in JavaScript than `rescue` does in Ruby. It seems like this code in Ruby: ``` begin do_something rescue => e handle_error e ensure log_something end ``` is equivalent to this in JavaScript: ``` try { doSomething(); } catch(e) { handleError(e); } finally { logSomething(); } ``` I'm not saying that they're 100% equivalent, but I was thinking of the `try`…

> The try-block would be way more useful if it could just define scope without being tied specifically to error handling. There are lots of circumstances where defining scope would be handy outside of conditionals and creating functions for a similar purpose.

This is the case today! With `let` and `const`, any {} block creates a scope. So:

    let foo = 'outside';
    {
      let foo = 'inside';
      console.log(foo); // logs 'inside'
    }
    console.log(foo); // logs 'outside'

Re: New Features in ES2019

#123

Earlier quoted context omitted.

> One thing I'd to request is that `Object.fromEntries()` simply skip undefined entries, rather than do...strange things. Not sure what strange things it does. fromEntries simply takes each entry, sets its first item as key (stringifying if necessary as object keys are necessarily strings) and the second item as value. Naturally falls from these that a null or undefined entry will error, and an empty entry will creat…

Yes, it errors out. And flatMap isn't applicable here. Here's some complete test code you can try in your browser right now: ``` let p = (a,fn) => Object.fromEntries(Object.entries(a).map(fn)) p({a:1, b:2}, ([k,v])=> v === 2 ? undefined : [k,v]) ``` My intent is to skip the entry with `b:2`. However, both map and flatMap error out. It would be nice if `fromEntries` did what I think is the appropriate thing, which is…

> And flatMap isn't applicable here.

It's absolutely applicable, flatMap can trivially act as a filterMap by returning an empty array in the "remove this element" case:

     let p = (a,fn) => Object.fromEntries(Object.entries(a).flatMap(fn))
     p({a:1, b:2}, ([k,v])=> v === 2 ? [] : [[k,v]])
there you go.

You can even make the adaptation transparent by wrapping the fn:

     let p = (a,fn) => Object.fromEntries(Object.entries(a).flatMap((v) => {
         let r = fn(v);
         return r == null ? [] : [r]; 
     }));
     p({a:1, b:2}, ([k,v])=> v === 2 ? undefined : [k,v])
> My intent is to skip the entry with `b:2`. However, both map and flatMap error out.

Return an empty list from flatmap to remove the entry and a singleton list containing the new pair to alter it.

> Is this not the behavior you'd expect?

No. I would much rather have the existing function which has a very clear and straightforward behaviour and is not prone to silently misbehaving on buggy code.

> Note that without special treatment in fromEntries, there's no way for the mapping function to indicate "skip this entry".

I fail to see an issue with that. If you want to remove an entry, remove the entry.

Post reply on HN