try/catch/else

Alan Plum me at pluma.io
Fri Feb 9 09:24:30 UTC 2018


I think the best argument for having try/catch/else is that it makes it trivial to translate promises into async/await. Consider this:

```
let result = a()
.then(b, c)
.catch(d);
```

If we want to translate this 1:1 to try/catch/else in an async function we'll end up with something like this:

```
try {
  let x, y;
  try {
    x = await a();
  } catch (e) {
    y = await c(e);
  } else {
    y = await b(x);
  }
  return y;
} catch (e) {
  return await d(e);
}
```

Doing this without `else` would require one of the workarounds suggested upthread.

On Thu, Feb 8, 2018, at 7:13 PM, Claude Pache 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);
> }
> ```
> 
> —Claude


More information about the es-discuss mailing list