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...
For further actions, you may consider blocking this person and/or reporting abuse
I think optional chaining is in typescript 3.7 for anyone interested
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 theallSettled
.Sound so much more succinct and readable!
No,
$.when()
is likePromise.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...Great! Thanks for the response!
Can someone give a quick overview of the benefits of
flatMap
? Im still struggling to get my head round a useful use-case.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.
Ahh I see, so it’s like
.map().flat()
?Makes sense, still only seeing limited use cases. But saves looping twice I guess.
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.
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.
Most language consider 0 to be falsy -_-
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.
Yes, but for example c++ just lets you if (number)
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.
Long live optional chaining