Last active
January 11, 2024 12:56
-
-
Save coderaiser/a26e535bc43b5fe1ac4d72624bd6bed2 to your computer and use it in GitHub Desktop.
Revisions
-
coderaiser renamed this gist
Oct 18, 2019 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # 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). @@ -137,6 +137,7 @@ 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 -
coderaiser revised this gist
Oct 18, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -136,6 +136,7 @@ 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 # Modules - https://www.npmjs.com/package/await-to-js -
coderaiser revised this gist
Oct 18, 2019 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ # One line try-catch 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: -
coderaiser revised this gist
Oct 18, 2019 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -124,6 +124,13 @@ like that. 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/ -
coderaiser revised this gist
Oct 18, 2019 . 1 changed file with 18 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -85,6 +85,24 @@ 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: -
coderaiser revised this gist
Oct 18, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ 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'); -
coderaiser revised this gist
Oct 18, 2019 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,6 +32,10 @@ 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? -
coderaiser revised this gist
Oct 18, 2019 . No changes.There are no files selected for viewing
-
coderaiser revised this gist
Oct 18, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -62,7 +62,7 @@ So this is more or less obvious for most developers. 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(); -
coderaiser revised this gist
Oct 18, 2019 . 1 changed file with 98 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,30 +1,4 @@ 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 whant is just more clear way to use try catch. All of them suggest to use: @@ -58,7 +32,103 @@ const {readFile} = require('fs').promises; const [error, data] = try await readFile('hello'); ``` # 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: ``` 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`. ## 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. # References - https://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/ # 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 -
coderaiser revised this gist
Oct 18, 2019 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -51,6 +51,13 @@ What I want to suggest is new operator `try` (it can use different name). And us 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'); ``` # References -
coderaiser revised this gist
Oct 18, 2019 . No changes.There are no files selected for viewing
-
coderaiser revised this gist
Oct 18, 2019 . 1 changed file with 27 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,5 +26,32 @@ If you search `npm` with query `try await` you will find a big modules list: - https://www.npmjs.com/package/eor - https://www.npmjs.com/package/eres What all this developers whant 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'); ``` # References - - https://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/ -
coderaiser revised this gist
Oct 18, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ If you search `npm` with query `try await` you will find a big modules list: - https://www.npmjs.com/package/await-to-js - https://www.npmjs.com/package/try-catch - https://www.npmjs.com/package/await-of -
coderaiser created this gist
Oct 18, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ If you search `npm` with query `try await` you will find a big modules list: - 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 # References - - https://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/