12 comments

  • bastawhiz2 minutes ago
    Be careful with this, though. If a promise is expected to resolve and it never does, and the promise needs to resolve or reject to clean up a global reference (like an event listener or interval), you'll create a memory leak. It's easy to end up with a leak that's almost impossible to track down, because there isn't something obvious you can grep for.
  • pjc502 hours ago
    I like how C# handles this. You&#x27;re not forced to support cancellation, but it&#x27;s strongly encouraged. The APIs all take a CancellationToken, which is driven by a CancellationTokenSource from the ultimate caller. This can then either be manually checked, or when you call a library API it will notice and throw an OperationCancelledException.<p>Edit: note that there is a &quot;wrong&quot; way to do this as well. The Java thread library provides a stop() function. But since that&#x27;s exogenous, it doesn&#x27;t necessarily get cleaned up properly. We had to have an effort to purge it from our codebase after discovering that stopping a thread while GRPC was in progress broke all future GRPC calls from all threads, presumably due to some shared data structure being left inconsistent. &quot;Cooperative&quot; (as opposed to preemptive) cancel is much cleaner.
    • teraflop1 hour ago
      I am surprised that you had to go out of your way to remove Thread.stop from existing Java code. It&#x27;s been deprecated since 1998, and the javadoc page explains pretty clearly why it&#x27;s inherently unsafe.<p>It&#x27;s hard to miss all the warnings unless you&#x27;re literally just looking at the method name and nothing else.
      • pjc501 hour ago
        I was certainly surprised to see it when I found it.
    • esprehn1 hour ago
      AbortSignal is same thing on the Web. It&#x27;s unfortunate TC39 failed to ever bring a CancelToken to the language to standardize the pattern outside browsers.
      • runarberg1 hour ago
        TC39 seems to be failing at many things for the past 10 years.
    • CharlieDigital1 hour ago
      C# has very good support for this.<p>You can even link cancellation tokens together and have different cancellation &quot;roots&quot;.
  • eithed1 hour ago
    &gt; Promise itself has no first-class protocol for cancellation, but you may be able to directly cancel the underlying asynchronous operation, typically using AbortController.<p><a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;Promise" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a>
  • mohsen11 hour ago
    Back in 2012 I was working on a Windows 8 app. Promises were really only useful on the Windows ecosystem since browser support was close to non existent. I googled &quot;how to cancel a promise&quot; and the first results were Christian blogs about how you can&#x27;t cancel a promise to god etc. Things haven&#x27;t changes so much since, still impossible to cancel a promise (I know AbortSignal exists!)
  • thomasnowhere18 minutes ago
    The never-resolving promise trick is clever but what caught me off guard is how clean the GC behavior is. Always assumed hanging promises would leak in long-lived apps but apparently not as long as you drop the references.
  • cush1 hour ago
    GC can be very slow. Relying on it for control flow is a bold move
    • augusto-moura29 minutes ago
      Not <i>that very</i> slow for web applications. Maybe for real time or time-sensitive applications. For most day to day web apps GC pauses are mostly unnoticeable, unless you are doing something very wrong
    • BlueGreenMagick42 minutes ago
      I don&#x27;t think the control flow relies on GC.<p>The control flow stops because statements after `await new Promise(() =&gt; {});` will never run.<p>GC is only relied upon to not create a memory leak, but you could argue it&#x27;s the same for all other objects.
    • dominicrose16 minutes ago
      as long as there&#x27;s no leak interrupting a promise should be good for performance overall, not necessarily for the front-end but for the whole chain.
  • abraxas1 hour ago
    and so the thirty year old hackathon continues...
  • dimitropoulos2 hours ago
    &gt; Libraries like Effect have increased the popularity of generators, but it&#x27;s still an unusual syntax for the vast majority of JavaScript developers.<p>I&#x27;m getting so tired of hearing this. I loved the article and it&#x27;s interesting stuff, but how many more decades until people accept generators as a primitive??<p>used to hear the same thing about trailing commas, destructuring, classes (instead of iife), and so many more. yet. generators still haven&#x27;t crossed over the magic barrier for some reason.
    • horsawlarway1 hour ago
      There just aren&#x27;t that many spots where the average js dev actually needs to touch a generator.<p>I don&#x27;t really see generators ever crossing into mainstream usage in the same way as the other features you&#x27;ve compared them to. Most times... you just don&#x27;t need them. The other language tools solve the problem in a more widely accessible manner.<p>In the (very limited &amp; niche) subset of spots you do actually need a generator, they&#x27;re nice to have, but it&#x27;s mostly a &quot;library author&quot; tool, and even in that scope it&#x27;s usage just isn&#x27;t warranted all that often.
      • no_wizard4 minutes ago
        mainly because they messed up on implementation, in two ways. This is of course my opinion.<p>The first being `.next()` on the returned iterators. If you pass an argument to it, the behavior is funky. The first time it runs, it actually doesn&#x27;t capture the argument, and then you can capture the argument by assigning `yield` to a variable, and do whatever, but its really clunky from an ergonomic perspective. Which means using it to control side effects is clunky.<p>The second one how it is not a first class alternative to Promise. Async Generators are not the most ergonomic thing in the world to deal with, as you have the issues above plus you have to await everything. Which I understand why, but because generators can&#x27;t be used in stead of Promises, you get these clunky use cases for using them.<p>They&#x27;re really only useful as a result, for creating custom iterator patterns or for a form of &#x27;infinite stream&#x27; returns. Beyond that, they&#x27;re just not all that great, and it often takes combining a couple generators to really get anything useful out of them.<p>Thats been my experience, and I&#x27;ve tried to adopt generators extensively a few times in some libraries, where I felt the pattern would have been a good fit but it simply didn&#x27;t turn out most of the time.
      • gbuk201342 minutes ago
        It is a specialised instrument but a useful one: batch processing and query pagination are first class use cases for generators that can really simplify business logic code. Stream processing is another and in fact Node.js streams have had a generator API for several releases now.
    • yeittrue2 hours ago
      Generators peaked in redux- saga and thunk days before we had widespread support for async&#x2F;await.<p>You&#x27;re right, mostly pointless syntax (along with Promise) now that we can await an async function anyway, especially now with for .. of to work with Array methods like .map<p>But there are still some use cases for it, like with Promise. Like for example, making custom iterators&#x2F;procedures or a custom delay function (sync) where you want to block execution.
  • game_the0ry2 hours ago
    Off topic, but that site has really nice design
    • williamdclt1 hour ago
      Mh, I couldn&#x27;t read due to the huge contrast and had to switch to reader mode, so...
      • seattle_spring32 minutes ago
        What colors were you seeing? It&#x27;s light white text on a black background for me-- both super common and plenty readable.
      • jazzypants1 hour ago
        I personally find it to be perfectly readable. I&#x27;ve heard of people with issues with white text on a black background, but I don&#x27;t fully understand it. Do you have astigmatism?
      • game_the0ry1 hour ago
        I mean, I&#x27;m not a designer but it was interesting enough to call out.
  • TZubiri1 hour ago
    If I know the javascript ecosystem, and I think I do, this is an opportunity for some undergrad from Kazakhstan to create a library called &#x27;Pinky&#x27; that offers unbreakable promises, which will have 1M downloads on npm and 10K stars on github, and will allow the dev to get a US Visa and employment.<p>The library will get additional maintainers until it balloons into 100Kloc with features like reading config files, which would need to eventually get split into a transitive dependency called configy, until one day a maintainer clicks on an enlarge penis link or gets phished by a fake AI girlfriend that was actually a russian dude, and it hits half of the javascript ecosystem because it had become a transitive dependency for every single package, and then everyone switches to a new package manager that somehow survived this due to a security feature, (but it was actually because no one uses that package manager and so the attacker didn&#x27;t target it) also it&#x27;s faster and used by the top startups of YC so it&#x27;s very sexy and it can now be your girlfriend so you don&#x27;t need a fake AI girlfriend and devs don&#x27;t get supply chained anymore.
    • seattle_spring31 minutes ago
      &gt; If I know the javascript ecosystem, and I think I do<p>You think you do, but...
  • afarah11 hour ago
    You can also race it with another promise, which e.g. resolves on timeout.