Destructuring assignment default value from promise

Viewed 182

When providing default values for destructuring assignments, something that I haven't seen spoken about is using await to provide the value from a promise, like so:

// Inside an asynchronous function, where `person` is an object...

const { 
  name = "Alex",
  age = await getAge(),
} = person;

I've tested this, and while it seems to work, I'm curious if it goes against some best-practices or if there is any other information out there as to why this shouldn't be done.

It's much handier than assigning to a mutable variable and re-initializing it later on, provided that async errors are caught correctly.

0 Answers
Related