try/catch/else

Claude Pache claude.pache at gmail.com
Fri Feb 9 09:35:50 UTC 2018



> Le 9 févr. 2018 à 00:19, Peter van der Zee <ecma at qfox.nl> a écrit :
> 
>>> On Thu, Feb 8, 2018 at 10:13 AM, Claude Pache <claude.pache at gmail.com>
>>> wrote:
>>>> 
>>>> What about the following pattern (labelled block + break)?
>>>> 
>>>> ```js
>>>> processSuggestions: {
>>>>    let suggestions;
>>>>    try {
>>>>      suggestions = await fetchSuggestions();
>>>>    } catch (e) {
>>>>      alert('Failed to load suggestions');
>>>>      break processSuggestions;
>>>>    }
>>>>    showSuggestions(suggestions);
>>>> }
>>>> ```
> 
> I don't mean to hijack this tread. I'm mostly curious why you opt or
> even suggest for that over putting it in a function and an early
> return? Like;
> 
> ```
> function processSuggestions() {
>  let suggestions
>  try {
>    suggestions = await fetchSuggestions();
>  } catch (e) {
>    return alert('Failed to load suggestions');
>  }
>  showSuggestions(suggestions);
> }
> ```
> 
> This is almost identical, especially the way the example was written.
> I understand the differences, I don't think they're a problem for by
> far most cases where you'd want this.

A function is strictly more complex than a block because you have to define it first, then to invoke it. As a consequence, it is more code to write and read. Unless you have a good reason for it (which is not suggested by the original problem), why would you prefer the more complex way?

—Claude



More information about the es-discuss mailing list