Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw and try/catch #2

Open
mhulse opened this issue Mar 1, 2019 · 13 comments
Open

Throw and try/catch #2

mhulse opened this issue Mar 1, 2019 · 13 comments

Comments

@mhulse
Copy link
Owner

mhulse commented Mar 1, 2019

Use just throw and try/catch around awaits:

if (this.options.orange == 'orange') {
// Instead of using `writeFile().then()`, use await:
await fs.writeFile(html, 'Hello world!', 'utf8');
let result = await fs.readFile(html, 'utf8');
await fs.unlink(html);
// Resolve this async function with the result:
return result;
} else {
throw new Error(`Orange isn’t orange, it’s ${this.options.orange}!`);

Here's example of just throw:

https://github.com/mhulse/get-tile-url/blob/f79b35889e0ad43fda12053cd1d2a1dcd8ef3b61/index.js#L90

@mhulse
Copy link
Owner Author

mhulse commented Mar 21, 2019

@mhulse
Copy link
Owner Author

mhulse commented Mar 23, 2019

@mhulse
Copy link
Owner Author

mhulse commented Mar 30, 2019

Good advice here: https://stackoverflow.com/a/52215925/922323

@mhulse
Copy link
Owner Author

mhulse commented Mar 30, 2019

This talks about how to use top-level caller:

https://stackoverflow.com/a/48685979/922323

@mhulse
Copy link
Owner Author

mhulse commented Mar 30, 2019

@mhulse
Copy link
Owner Author

mhulse commented Mar 30, 2019

This is interesting:

https://makandracards.com/makandra/43653-javascript-don-t-throw-exceptions-from-async-functions

TLDR: A function is hard to use when it sometimes returns a promise and sometimes throws an exception. When writing an async function, prefer to signal failure by returning a rejected promise.

@mhulse
Copy link
Owner Author

mhulse commented Mar 30, 2019

I need to make a readme wiki repo on exception handling, just for my own education. This shit is probably one of the more complex/ambiguous feature(?) of es6+ (in my mind).

Just need more practice. :)

@mhulse
Copy link
Owner Author

mhulse commented Mar 31, 2019

Looks like thowing a new error will result in a rejected promise in a async/await.

https://stackoverflow.com/a/42453705/922323

@mhulse
Copy link
Owner Author

mhulse commented Mar 31, 2019

A few good paragraphs about constructors and async operations; basically, don't do that in constructor.

@mhulse
Copy link
Owner Author

mhulse commented Apr 4, 2019

Great tips here:

https://eloquentjavascript.net/08_error.html

Another way to set a breakpoint is to include a debugger statement (consisting of simply that keyword) in your program. If the developer tools of your browser are active, the program will pause whenever it reaches such a statement.

If you’re programming only for yourself, you can afford to just ignore such problems until they occur. But if you build something that is going to be used by anybody else, you usually want the program to do better than just crash. Sometimes the right thing to do is take the bad input in stride and continue running. In other cases, it is better to report to the user what went wrong and then give up. But in either situation, the program has to actively do something in response to the problem.

@mhulse
Copy link
Owner Author

mhulse commented Apr 4, 2019

@mhulse
Copy link
Owner Author

mhulse commented Apr 6, 2019

Good tips here:

https://stackoverflow.com/a/42171508/922323

@mhulse
Copy link
Owner Author

mhulse commented Apr 6, 2019

Returning a promise from async will not double wrap the promise:

https://stackoverflow.com/a/35302535/922323

Non-promise return will be wrapped.

No return will return a promise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant