been helping someone with some JS and just realised the interaction between async/await and arrow function return semantics is quite bad
-
Show this thread
-
(id) => put(url, data) // congrats you can await on this for the request to complete (id) => { put(url, data) } // race condition printer go brrr
4 replies 7 retweets 31 likesShow this thread -
you won't get any feedback from the language that this is wrong, you just get race conditions that are hard to debug, because you put a brace around something
3 replies 0 retweets 20 likesShow this thread -
this is really hard to spot
2 replies 0 retweets 8 likesShow this thread -
especially if the put(...) call is multi-line and someone may have simply put it on a separate line from the =>, or surrounded it with parens, or with braces
1 reply 0 retweets 5 likesShow this thread -
like, I saw this b/c I've spent a lot of time knowing what edges to look for in async JS code, but it's really unfriendly to beginners
1 reply 0 retweets 10 likesShow this thread -
also, you can `await` non-promise values; `await x` is effectively `Promise.resolve(x).then(...)`. this masks a lot of bugs
2 replies 3 retweets 15 likesShow this thread -
this problem with brace vs no-brace manifests with any other return type, but it'll usually be easier to spot b/c you'll call some method on the result, so if: fn = (id) => { value } then fn().foo() will give an exception: no method foo() for undefined
1 reply 0 retweets 5 likesShow this thread -
async/await makes this harder to spot because it's maximally forgiving -- `await` accepts non-Promise values, which is to say, all values, without exception
5 replies 1 retweet 11 likesShow this thread -
Replying to @mountain_ghosts
Oof, Rust's IntoFuture trait goes in the same direction, not sure it's a good idea to add that after all
2 replies 0 retweets 0 likes
We wouldn't `impl<T> IntoFuture for T` tho — that seems like it'd be the most equivalent to what JS does, but would also defeat the purpose of the trait entirely.
-
-
Replying to @yoshuawuyts @sheevink
yes right, x.await is only defined for x: IntoFuture whereas in JS it's defined for all x
0 replies 0 retweets 0 likesThanks. Twitter will use this to make your timeline better. UndoUndo
-
-
-
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.