TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A brief look at async-await

37 pointsby sveingjobyover 5 years ago

6 comments

sloonzover 5 years ago
<p><pre><code> check() .then(result =&gt; { if (result) { &#x2F;&#x2F; set state to finished } check() .then(result =&gt; { if (result) { &#x2F;&#x2F; set state to finished } check() .then(result =&gt; { if (result) { &#x2F;&#x2F; set state to finished } check() .then(result =&gt; { if (result) { &#x2F;&#x2F; set state to finished } check() .then(result =&gt; { if (result) { &#x2F;&#x2F; set state to finished } &#x2F;&#x2F; set state to not done }) .catch(error =&gt; &#x2F;&#x2F; set state to failed); }) .catch(error =&gt; &#x2F;&#x2F; set state to failed); }) .catch(error =&gt; &#x2F;&#x2F; set state to failed); }) .catch(error =&gt; &#x2F;&#x2F; set state to failed); }) .catch(error =&gt; &#x2F;&#x2F; set state to failed); </code></pre> For the love of everything that’s sacred, please don’t do this. The strength of promises is that they can free us from that exact kind of callback hell, and they do that by being chainable.<p><pre><code> const resultOrNull = await Promise.resolve(null). then(result =&gt; result || check()). then(result =&gt; result || check()). then(result =&gt; result || check()). then(result =&gt; result || check()). then(result =&gt; result || check()). catch(error =&gt; null); </code></pre> Calling .then&#x2F;.catch inside of a .then&#x2F;.catch is a huge red flag. Almost always, you want to return the promise and chain instead.
评论 #21743172 未加载
评论 #21743154 未加载
评论 #21751834 未加载
cyrusmgover 5 years ago
There should be some review process for these .christmas articles. I get it - creating tons of articles quickly is hard, but the core message should not be incorrect.<p>I tried to look up a github reference on the page, so I could send a PR to fix the linked article, but it is not available.
评论 #21741945 未加载
laurent123456over 5 years ago
The article would have been better without the last example. It doesn&#x27;t clarify much and it&#x27;s not something that anyone should ever write.
评论 #21743205 未加载
quietbritishjimover 5 years ago
Presumably the &quot;&#x2F;&#x2F; set state to finished&quot; involves a return statement, otherwise &quot;check()&quot; will continue to be called and ultimately the state will be set back to not done. This applies to both the async version and the chain() version. Personally I find it confusing that the comment hides a control flow statement when the text of the comment suggests that it&#x27;s just setting a state variable.<p>Surely a better comparison for the async function would be a chain() that manually recreates the loop using a parameter or local captured variable, like shown below. They&#x27;re still complex but if anything show the complexity more clearly, especially if 6 is not hardcoded but could be passed as a parameter. I suppose having the unrolled version from the article plus one of these would be best of all, to show the problem from all angles.<p><pre><code> function chainWithParam() { var checkAndIterate; checkAndIterate = (result, i) =&gt; { if (result) { &#x2F;&#x2F; set state to finished return; } if (i == 6) { &#x2F;&#x2F; set state to not done return; } check() .then(result =&gt; checkAndIterate(result, i + 1)) .catch(error =&gt; &#x2F;* set state to failed *&#x2F;); } checkAndIterate(false, 0); } function chainWithCapturedLocal() { i = 0; var checkAndIterate; checkAndIterate = result =&gt; { if (result) { &#x2F;&#x2F; set state to finished return; } if (i == 6) { &#x2F;&#x2F; set state to not done return; } ++i; check() .then(checkAndIterate) .catch(error =&gt; &#x2F;* set state to failed *&#x2F;); } checkAndIterate(false); } </code></pre> I&#x27;m not a JavaScript programmer so there could be mistakes in the above code. I&#x27;d be interested to know if there is, especially whether it was really necessary to declare the checkAndIterate variable on a separate line to its assignment.<p>Surely the &quot;manual&quot; function could be implemented with some sort of loop-like
评论 #21743270 未加载
gbuk2013over 5 years ago
I appreciate that the author is trying to illustrate something with an intentionally contrived example, but there is really no need for &quot;await&quot; in his &quot;chain()&quot; function:<p><pre><code> function chain () { (function loop (i) { if (i &gt; 5) { &#x2F;&#x2F; set state to failed return; } check().then((result) =&gt; { if (!result) { return loop(i + 1); } &#x2F;&#x2F; set state to finished }).catch((error) =&gt; { &#x2F;&#x2F; set state to failed }); }(0)); } </code></pre> This is one of my pet peeve with proponents of promises actually. They come up with convoluted examples to argue against callbacks but it&#x27;s not the callbacks that are a problem, but the person writing the code.<p>Actually, I would argue that promises really made no sense until &quot;await&quot; became available because they introduce extra complexity and (small) performance penalty for no added benefit. Worse, it encourages less experienced developers to write code in a serial manner in situations where this is not necessary (a much bigger performance problem).<p>Even with &quot;await&quot;, the only time when it is really useful is when there have to be a several asynchronous operations that must happen serially because they use the result of the previous call. It depends on your field of work, of course, but in my experience these situations are really not that common.<p>Some time ago I wrote an article comparing performance of callbacks vs async&#x2F;await but it is also an example to show that callback code does not have to be more tedious to write:<p><a href="https:&#x2F;&#x2F;gir.me.uk&#x2F;posts&#x2F;node-8-async-await-performance-test.html" rel="nofollow">https:&#x2F;&#x2F;gir.me.uk&#x2F;posts&#x2F;node-8-async-await-performance-test....</a>
评论 #21742116 未加载
评论 #21742230 未加载
评论 #21742220 未加载
TheSoftwareGuyover 5 years ago
I know its kind of meta, but what&#x27;s with all the blog posts lately hosted on a *.christmas domain? These posts seem to have nothing to do with the holiday<p>I&#x27;ll admit the only reason I&#x27;ve noticed the domain is my corporate IT seems to have all sites on weird TLDs blocked by default :&#x2F;
评论 #21743067 未加载
评论 #21743044 未加载