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.

Stop Using IsLoading Booleans

28 pointsby valtismabout 5 years ago

2 comments

_bxg1about 5 years ago
I have mixed feelings about this. In a language with proper enums like Rust this is certainly the way to go, because your entire data structure can be tied to which state you&#x27;re in (Success would <i>only</i> contain the position, Rejected would <i>only</i> (and always) contain an error, Loading would <i>never</i> contain redundant data). In Rust you would also get the benefit of having match { } make sure you always take care of every possible variant.<p>In JavaScript, on the other hand, code could just as easily set an error and forget to set the status as set an error and forget to clear the position. There&#x27;s still some overlap between state info and there&#x27;s not a singular, clear winner between the two patterns.<p>Here&#x27;s what I would do for this case: first, move the error rendering clause above the position one. Erroring, like loading, is an exceptional status above the &quot;normal&quot; status. Therefore the position should be the &quot;fallthrough&quot; branch. This change alone would fix the actual problem demonstrated.<p>Then, to help avoid inconsistencies, I like to put my &quot;isLoading: false&quot; in a &quot;finally&quot; clause (if it&#x27;s on a Promise), or some equivalent. Unfortunately watchPosition is a bespoke function with regular callbacks instead of a promise, but you could still centralize the loading flag clearing in some way. Or maybe &quot;promisify&quot; that function with a new Promise((res, rej) =&gt; {}).<p>Certainly if you have more than just these three statuses you start getting into territory where a status string makes more and more sense. However, these three (success, error, loading) are special, and in a large number of cases they&#x27;re all you have, so they can be afforded some special treatment.
评论 #22492613 未加载
Nullabillityabout 5 years ago
At this point I&#x27;m starting to think that every state framework should ship with something like Diode&#x27;s Pot[0] (which this post also reimplements, though with more strings and without the clear state diagram).<p>[0]: <a href="https:&#x2F;&#x2F;diode.suzaku.io&#x2F;advanced&#x2F;Pot.html" rel="nofollow">https:&#x2F;&#x2F;diode.suzaku.io&#x2F;advanced&#x2F;Pot.html</a>