When calling an async function that returns a value, the following code is currently required (on top of having async in the function definition of course):
try {
let myVar = await somePromiseFunction(blah);
} catch (e) {
console.error(e);
}
I somewhat expected, hoped, and was disapointed when it wasn't available, that I could do the following instead:
let myVar = await somePromiseFunction(blah).catch(console.error);
Obviously this offers very minimal advantage when chaining a lot of promises one after the other. However, for single-line statements, I believe it's still readable enough, and gives the best of both worlds.
What happens? It should be possible to do this
I don't think there is anything that we can do about this. You would probably need to submit an issue on the V8 issue tracker.
@vkurchatkin From my tests, at least in version 7 with the harmony flag, the .catch() is simply ignored. Maybe it's just me, but it does feel like it would be more "elegant" to be able to do it the way I describe, in _some_ situations.
/cc @gsathya
d8> (async function() { try { let v = await Promise.reject('this is printed'); } catch(e) { print(e) ;}})()
this is printed
[object Promise]
d8> (async function() { let v = await Promise.reject('this is printed').catch(val => print(val)); })()
this is printed
[object Promise]
Seems like working as expected to me. Did you expect a different behavior?
@eslachance you can already .catch when using await. It would be silly for them to remove the ability to try/catch.
Maybe do some further testing.
When calling an async function that returns a value, the following code is currently required (on top of having async in the function definition of course)
There's nothing required, you can leave the try/catch statement out if your function is guaranteed not to throw an exception (that being said, you could leave it out nonetheless, even if it might be throwing. You just have to expect warnings, and in the future a possible termination of your process).
Obviously this offers very minimal advantage when chaining a lot of promises one after the other. However, for single-line statements, I believe it's still readable enough, and gives the best of both worlds.
Not true. You can do the following:
try {
const a = await foo();
const b = await bar();
const c = await foobar();
} catch (e) {
console.error(e);
}
which is equivalent to:
foo().then(a => bar()).then(b => foobar()).then(c => ...).catch( e => console.error(e) );
I have definitely failed at my test, I guess I did it much too quickly and not thoroughly enough. I hang my head in shame. Sorry for wasting your time.
@eslachance
Don't be too hard on yourself! async/await is fairly new in the JavaScript spec. Believe me, the vast majority of JavaScript people out there don't even know of its existence (yet) - or, if they do, wouldn't know how it's supposed to work. Even w/o async functions, people struggle with Promise resolutions, and even callbacks. So, believe me, you're already ahead of almost everyone just trying to put async functions to work ;)