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:
const tryCatch = require('try-catch');
const [error, data] = tryCatch(JSON.parse, 'hello');Instead of
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:
const [error, data] = try JSON.parse('hello');When we have a promise, we can use try this way:
const {readFile} = require('fs').promises;
const [error, data] = try await readFile('hello');This is also a pattern used in a Go language.
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.
Because when you use object destructuring you chose names of variables, for example:
const {error, data} = try await readFile('hello');
const {error: error2, data: data2} = try await readFile('hello2');This can very fast became a mess.
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:
fs.readFile('hello.txt', (error, data) => {
// you should check if error happend first
});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:
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:
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();
}Because this produces more code thet can be used, make this harder to read, consider this example:
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.
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.
Because you should fullfil the promise first and then handle it's result. Similar to:
typeof await promise();- 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
- 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