DEV Community

Cover image for The future of Javascript - features to keep an eye on

The future of Javascript - features to keep an eye on

Christopher Kade on October 24, 2019

We take a lot of Javascript features for granted, map, filter, reduce, const/let, ternaries... each one of these had a major impact on our code bas...
Collapse
 
aaron_powell profile image
Aaron Powell

I think optional chaining is in typescript 3.7 for anyone interested

Collapse
 
victorcorcos profile image
Victor Cordeiro Costa • Edited

Looks like Promise.allSettled is the same as $.when() which is provided by jQuery, am I correct?
Both of them returns a Promise that will be executed when the inner promises are done.

I prefer 200% the when syntax than the allSettled.

Promise.when([promise1, promise2]).done...
Enter fullscreen mode Exit fullscreen mode

Sound so much more succinct and readable!

Collapse
 
nlepage profile image
Nicolas Lepage

No, $.when() is like Promise.all(), they both fail/reject as soon as one of the given promises rejects.
Promise.allSettled will resolve when all given promises have either resolved or rejected, and then you have to manually check the results...

Collapse
 
victorcorcos profile image
Victor Cordeiro Costa

Great! Thanks for the response!

Collapse
 
georgecoldham profile image
George

Can someone give a quick overview of the benefits of flatMap? Im still struggling to get my head round a useful use-case.

Collapse
 
wormss profile image
WORMSS • Edited

Let's say you have a bunch of objects, let's say they are People{}, and they have an array of children[].

If you wanted the list of children, of an array of people, you could use flat map.

people.flatMap(p => p.children)

So you return the array of children, and all those arrays get flattened into 1 giant array.

Collapse
 
georgecoldham profile image
George

Ahh I see, so it’s like .map().flat()?

Makes sense, still only seeing limited use cases. But saves looping twice I guess.

Thread Thread
 
wormss profile image
WORMSS

Might be dependant upon your projects you do. We deal with a lot of nested dynamic data at work, we use this technique a lot.

Collapse
 
johnkazer profile image
John Kazer

I try to use Ramda lenses to create a sort of interface to objects, so my code doesn't really need to know the details of the structure. You can also protect against missing data and generate default return values.

So I would consider optional chaining to be a bit of a hack that a more functional approach to JavaScript can already solve more cleanly.

ramdajs.com/docs/#lens
ramdajs.com/docs/#pathOr
etc.

Collapse
 
mateiadrielrafael profile image
Matei Adriel

Most language consider 0 to be falsy -_-

Collapse
 
seanmclem profile image
Seanmclem

In C# you had to parse it to a boolean to test if it was false. Last time I used it. Like how you can't just drop a string into an If-condition, you have to test if it's null or empty, but not with js.

Collapse
 
mateiadrielrafael profile image
Matei Adriel • Edited

Yes, but for example c++ just lets you if (number)

Collapse
 
dahmon profile image
Dahmon Bicheno

Optional chaining and nullish coalescing are going to be absolute life savers!
Definitely the two features I will most often use, honestly surprised they haven't been introduced earlier.

Collapse
 
shadowwarior5 profile image
Konstantin Meiklyar

Long live optional chaining