DEV Community

Discussion on: What's new in JavaScript - ES2020

Collapse
gautemeekolsen profile image
Gaute Meek Olsen Author • Edited on

I agree with you there 😊

For the last two points, they might be useful for some use cases. But like you say, you might not need it and use what you describe.

  • Promise.all might not be sufficient if you need the value from the promises that resolves. Here is an example where Promise.all will log the error and not be able to get the result from the resolved promises. The Promise.allSettled will be able to get the result from the resolved promises. But again, this is only for use cases when you don't need all promises to resolve to continue.
// Promise.all with try catch
try{
  const [r1, r2] = await Promise.all([Promise.resolve(1), Promise.reject(2)])
  console.log({r1, r2}) // never runs and we won't get r1 value
}catch(error){
  console.log({error}) // logs error
}

// Promise.allSettled
const [r1, r2] = await Promise.allSettled([Promise.resolve(1), Promise.reject(2)])
console.log({ r1, r2 }) // can use r1 even though r2 rejected
  • Nullish coalescing can be useful to avoid default values for falsy values that should be set. Think of this example where a user has set preferred sound-level to zero, we don't want to overwrite this with the default sound value which is 50. Which, we only want for users who have never set the preferred sound-level.
const user = { preferredSound: 0 }
let sound = user.preferredSound ?? 50 // value is 0
let soundWrong = user.preferredSound || 50 // value is 50

edit: updated article with these examples to clarify.