Re:Go 2 Draft Designs
by reggieband on Tuesday August 28, 2018 @06:38PM
(#17863566)
Maybe I'm crazy but I'd much prefer that as:
const temp = await foo;
const bar = await temp.bar;
const baz = await bar.baz;
But then again it looks like in this example we are returning promises from getters on objects which is something I would avoid.
Re:Go 2 Draft Designs
by munificent on Tuesday August 28, 2018 @07:14PM
(#17863756)
Yeah, the guidance is usually to hoist the subexpressions out to variables like you do here. But I encounter it often enough that it feels like we are trying to paper over a poor language syntax.
> we are returning promises from getters on objects which is something I would avoid.
Getters are very common in Dart and it's idiomatic to use them even for properties that are asynchronous. (It's not like making it a method with an extra `()` really solves anything.) It's not as common to have nested chains of asynchronous properties, but they do happen, especially in tests.
Would it be better to use statements more? After all, this is a sequence of steps and it's doing a context switch between each step. That seems better written vertically, one per line.
Deeply chained method calls are a code smell on their own. But would it really be that much better if you wrote it as something like:
foo..bar..baz;I'm not sure about Dart, but in my experience it's rare that you await a function which returns an object with another awaitable in it.
Never mind something that nests these 2 levels deep.
Re:Go 2 Draft Designs
by RodericDay on Tuesday August 28, 2018 @07:19PM
(#17863797)
fetch() + response.text() is usually two. add a user-defined third and you're there.
In Dart (as in a couple of other languages) we have an `await` keyword for asynchrony. It has the same problem you describe and it really is very painful. Code like this is not that uncommon:
It sucks. I really wish the language team had gone with something postfix. For error-handling, I wouldn't be surprised if chaining like this was even more common.