# Try-catch oneliner If you search `npm` with query `try await` you will find a big modules list (you can find it in the boottom). What all this developers want is just more clear way to use try catch. All of them suggest to use: ```js const tryCatch = require('try-catch'); const [error, data] = tryCatch(JSON.parse, 'hello'); ``` Instead of ```js let error, data; try { data = JSON.parse('hello'); } catch (e) { error = e; } ``` Because this is much more clean way to handle things. We get less code in a more readable form. What I want to suggest is new operator `try` (it can use different name). And used this way: ```js const [error, data] = try JSON.parse('hello'); ``` When we have a promise, we can use `try` this way: ```js const {readFile} = require('fs').promises; const [error, data] = try await readFile('hello'); ``` This is also a pattern used in a [Go language](https://www.reddit.com/r/golang/comments/9t7con/why_no_trycatch_in_golang_whats_the_theory_behind/e8ukzmv/). This construction can be used as a library. But it so clean and powerful, why not just add it the language to a void producing more and more the same libraries. # FAQ ## Why array destructuring used? Because when you use object destructuring you chose names of variables, for example: ```js const {error, data} = try await readFile('hello'); const {error: error2, data: data2} = try await readFile('hello2'); ``` This can very fast became a mess. ## Why first value is error? Because you will not always have second argument, and because you always should check if error happend. This is similar to node.js callback style: ```js fs.readFile('hello.txt', (error, data) => { // you should check if error happend first }); ``` So this is more or less obvious for most developers. ## Isn't this is a problem that only one function is a try block? No. Because when you use `async-await` in most places you can avoid `try catch blocks`, but you should use it in the main function to handle errors: ```js async main() { await start(); await doSomething(); await end(); } // will be possible with top-level-await try { await main(); } catch(e) { console.log(e); } // or just main().catch(console.log) ``` When error occures in one of function calls: `start`, `doSomething`, `end` we will catch it in `main().catch`. Also such things gives ability to developer use `try` when necessary to change the way things going, for example: ```js async main() { const [startError] = try await start(); if (startError.message === 'critiq') throw startError; const [someError] = await doSomething(); if (someError.message !== 'done already') throw someError; await end(); } ``` ## Why no just keep using big old try-catch block? Because this produces more code thet can be used, make this harder to read, consider this example: ```js async main() { try { await start(); await doSomething(); await end(); } catch (e) { } } ``` You have more indentation, more code constructs, and everything looks like a mess, and a lot module authors thinks like that. ## Why new operator? Because `try` can be used in a similar way as `typeof`, `await`, etc. It's and maximizes the potential of destructuring assignment making the code more readable obvious, clean and simple. ## Why try before await? Because you should fullfil the promise first and then handle it's result. Similar to: ```js typeof await promise(); ``` # References - https://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/ - https://blog.golang.org/error-handling-and-go - https://stackoverflow.com/questions/5126560/try-catch-oneliner-available # Modules - https://www.npmjs.com/package/await-to-js - https://www.npmjs.com/package/try-catch - https://www.npmjs.com/package/await-of - https://www.npmjs.com/package/await-result - https://www.npmjs.com/package/@particle/await-on - https://www.npmjs.com/package/@zmotivat0r/o0 - https://www.npmjs.com/package/await-on - https://www.npmjs.com/package/await-plz - https://www.npmjs.com/package/await-call - https://www.npmjs.com/package/better-async-await.macro - https://www.npmjs.com/package/a-promise-wrapper - https://www.npmjs.com/package/onawait - https://www.npmjs.com/package/eres - https://www.npmjs.com/package/a-wait-forit - https://www.npmjs.com/package/eor - https://www.npmjs.com/package/catchify - https://www.npmjs.com/package/better-try-catch - https://www.npmjs.com/package/async-nocatch - https://www.npmjs.com/package/omega-fn - https://www.npmjs.com/package/flatasync - https://www.npmjs.com/package/error-first - https://www.npmjs.com/package/async-nocatch - https://www.npmjs.com/package/await-promise-wrapper - https://www.npmjs.com/package/eor - https://www.npmjs.com/package/eres