Re:Emerging JavaScript pattern: multiple return values
by noman-land on Thursday October 29, 2020 @10:40AM
(#24930937)
You probably already know this but you can destructive just the second half as well.
`cont [, err] = validate(data)`
1. I know anything returned `err` is safe to display back to the user
2. Using the array (as opposed to an object) can allow me to name the values when I destructure so it can be used multiple times in the same block with useful naming.
3. I can destructure just the first half `[isValid]` if that’s all I care about.
4. More generally it helps keep the code readable. Other control flows might rely on some casting, assumptions, or exceptions but this way presents a nice customizable api for users writing the code, and a readable api for those reviewing the code.