A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. title: Why I’m Not Waiting On ‘await’ (part 2)
  2. url: https://blog.getify.com/not-awaiting-2
  3. hash_url: dc98d4cae65338a1be618bab8fa43177
  4. <div>
  5. <p>Two-part series on the highs and lows of the coming <code>async..await</code> in JS:</p>
  6. <ul>
  7. <li><a href="https://blog.getify.com/not-awaiting-1">Part 1</a>: what <code>async..await</code> is and why it’s awesome</li>
  8. <li><a href="https://blog.getify.com/not-awaiting-2">Part 2</a>: why <code>async..await</code> falls short compared to generators+promises</li>
  9. </ul>
  10. </div>
  11. <hr/>
  12. <hr/>
  13. <p>In <a href="https://blog.getify.com/not-awaiting-1/">part one</a>, I covered why <code>async..await</code> has built up such momentum on the hype train. The promise of sync-looking async is powerfully attractive.</p>
  14. <p>Now I’ll cover why I think we should slow that train down. A bunch.</p>
  15. <p>I’m going to make a case for sticking with generators+promises by exploring some shortcomings of <code>async..await</code>. As I’ve already shown, the syntax of generators+promises isn’t that much worse. But they’re more powerful.</p>
  16. <h3 id="missing-direct-delegation">Missing Direct Delegation</h3>
  17. <p><a href="https://blog.getify.com/not-awaiting-1/#a-rose-by-any-other-name">Remember in part one</a> where I asserted that <code>yield</code> is more about control transfer than value semantics? That’s more obvious once we look at <code>yield \*</code>.</p>
  18. <p>In a generator, a <code>yield</code> transfers control back to the calling code. But what if we want to delegate to another piece of code, while still preserving the pause/resume async semantics.</p>
  19. <p>We can instead do a <code>yield \*</code> delegation to another generator, like this:</p>
  20. <pre class="code">
  21. function \*foo() {
  22. var x = yield getX();
  23. var y = yield \* bar( x );
  24. console.log( x, y );
  25. }
  26. function *bar(x) {
  27. var y = yield getY( x );
  28. var z = yield getZ( y );
  29. return y + z;
  30. }
  31. run( foo() );
  32. </pre>
  33. <p>You'll notice that the <code>yield \* bar(..)</code> lets the <code>bar()</code> generator run. But <strong>how</strong> does it run? We didn't use the <code>run(..)</code> utility on that call.</p>
  34. <p>We don't have to, because <code>yield \*</code> actually <em>delegates control</em>, so the original <code>run(..)</code> from the last line of the snippet is now transparently controlling (pausing/resuming) the <code>bar()</code> generator as it proceeds, without even knowing it. Once <code>bar()</code> completes, its return value (if any) finally resumes the <code>yield \*</code> with that value, returning execution to the <code>foo()</code> generator.</p>
  35. <p>The <code>yield \*</code> can even be used to create a sort of generator recursion, where a generator instance delegates to another instance of itself.</p>
  36. <p>Because <code>async function</code>s are not about control transfer, there's no equivalent to <code>yield \*</code> like <code>await \*</code>. There <em>was</em> talk about appropriating the <code>await \*</code> syntax to mean something like <code>await Promise.all(..)</code>, but that was eventually rejected.</p>
  37. <p>The <code>async function</code> equivalent would be:</p>
  38. <pre class="code">
  39. async function foo() {
  40. var x = await getX();
  41. var y = await bar( x );
  42. console.log( x, y );
  43. }
  44. async function bar(x) {
  45. var y = await getY( x );
  46. var z = await getZ( y );
  47. return y + z;
  48. }
  49. foo();
  50. </pre>
  51. <p>We don't need a <code>yield \*</code>-type construct here because <code>bar()</code> creates a promise we can <code>await</code>.</p>
  52. <p>That may seem that it's basically just, again, nicer syntax sugar removing the need for the <code>*</code>. But it's subtly different. With the <code>yield \*</code>, we're not creating a wrapper promise but rather just passing control directly through.</p>
  53. <p>What if <code>bar()</code> has some case where it can provide its result right away, such as pulling from a cache/memoization?</p>
  54. <pre class="code">
  55. function \*bar(x) {
  56. if (x in cache) {
  57. return cache[x];
  58. }
  59. var y = yield getY( x );
  60. var z = yield getZ( y );
  61. return y + z;
  62. }
  63. </pre>
  64. <p>versus:</p>
  65. <pre class="code">
  66. async function bar(x) {
  67. if (x in cache) {
  68. return cache[x];
  69. }
  70. var y = await getY( x );
  71. var z = await getZ( y );
  72. return y + z;
  73. }
  74. </pre>
  75. <p>In both cases, we immediately <code>return cache[x]</code>.</p>
  76. <p>In the <code>yield \*</code> case, <code>return</code> immediately terminates the generator instance, so we'd get that result back in <code>foo()</code> right away. But with promises (which is what <code>async function</code>s return), unwrapping is an async step, so we have to wait a tick before we can get that value.</p>
  77. <p>Depending on your situation, <strong>that may slow your program down a little bit</strong>, especially if you're using that pattern a lot.</p>
  78. <h3 id="cant-be-stopped">Can't Be Stopped</h3>
  79. <p>In part one, <a href="https://blog.getify.com/not-awaiting-1/#a-rose-by-any-other-name">I asserted</a> that <code>await</code> is declarative while <code>yield</code> is imperative. Pushing that to a higher level, <code>async function</code>s are declarative and generators are imperative.</p>
  80. <p>An <code>async function</code> is represented by (ie, returns) a promise. This promise represents one of two states: the whole thing finished, or something went wrong and it stopped. In that sense, the <code>async function</code> is an all-or-nothing proposition. But moreover, because it's a promise that's returned, all you can do is observe the outcome, not control it.</p>
  81. <p>By contrast, a generator is represented by (ie, returns) an iterator. The iterator is an external control for each of the steps in the generator. We don't just observe a generator's outcome, we participate in it and control it.</p>
  82. <p>The <code>run(..)</code> function that drives the generator+promise async flow control logic is actually negotiating via the iterator.</p>
  83. <p>The <code>next(..)</code> command on the iterator is how values are sent into the generator, and how values are received from it as it progresses. The iterator also has another method on it called <code>throw(..)</code>, which injects an exception into the generator at its paused position; <code>next(..)</code> and <code>throw(..)</code> pair together to signal (from the outside) success or failure of any given step in the generator.</p>
  84. <p>Of course, there's no requirement that the code controlling a generator's iterator choose to keep it going with <code>next(..)</code> or <code>throw(..)</code>. Merely opting to stop calling either has the effect of indefinitely pausing the generator. The iterator instance could be left in this paused state until the program ends or it's garbage collected.</p>
  85. <p>Even better, a generator's iterator has a third method on it that can be useful: <code>return(..)</code>. This method terminates the generator at its current paused point; you can send an affirmative signal into the generator from the outside telling it to stop right where it sits.</p>
  86. <p>You can even detect and respond to this <code>return(..)</code> signal inside the generator with the <code>finally</code> clause of a <code>try</code> statement; a <code>finally</code> will always be executed, even if the statement inside its associated <code>try</code> was terminated from the outside with an iterator's <code>return(..)</code> method.</p>
  87. <p>A <code>run(..)</code> that was slightly smarter could offer the advantage of allowing a generator to be stopped from the outside.</p>
  88. <p>Imagine a scenario where midway through a generator's steps, you can tell by the values emitted that it's appropriate to stop the progression of the generator -- to cancel any further actions. Maybe the generator makes a series of Ajax calls one after the other, but one of the results signals that none of the others should be made.</p>
  89. <p>Consider a more capable <code>run(..)</code> used like this:</p>
  90. <pre class="code">
  91. run( main(), function shouldTerminate(val){
  92. if (val == 42) return true;
  93. return false;
  94. } );
  95. </pre>
  96. <p>Each time a value is ready to be sent in to resume the <code>main()</code> generator, this <code>shouldTerminate(..)</code> function is first called with the value. If <em>it</em> returns <code>true</code>, then the iterator is <code>return(..)</code>d, otherwise the value is sent via <code>next(..)</code>.</p>
  97. <p>A generator provides the power of external cancelation capability whereas an <code>async function</code> can only cancel itself. Of course, the necessary cancelation logic can be baked into the <code>async function</code>, but that's not always appropriate.</p>
  98. <p>It's often much more appropriate to separate the async steps themselves from any logic that will consume or control (or cancel) them as a result. With generators that is possible, but with <code>async function</code>s it's not.</p>
  99. <h3 id="scheduling-is-hidden">Scheduling Is Hidden</h3>
  100. <p>It's tempting to assume that a program should always complete its async steps ASAP. It may be hard to conceive of a situation where you wouldn't want some part of the program to proceed <em>right away</em>.</p>
  101. <p>Let's consider a scenario where we have more than one set of asynchronous logic operating at the same time. For example, we might be making some HTTP requests and we might also be making some database calls. For the sake of this discussion, let's assume that each set of behavior is implemented in its own <code>async function</code>.</p>
  102. <p>Because <code>async function</code>s essentially come with their own <code>run(..)</code> utility built-in, the progression of steps is entirely opaque and advances strictly in ASAP fashion.</p>
  103. <p>But what if you needed to control the scheduling of the resumptions of each task's steps? What if, for the purposes of throttling resource usage, you needed to make sure that no more than one HTTP request/response was handled for each database request?</p>
  104. <p>Another problem could be that one set of async steps might have each step proceed so quickly (essentially immediately) that this task could "starve" the rest of the system by not letting any other tasks have a chance. To understand this possibility, you should read more about <a href="https://github.com/getify/You-Dont-Know-JS/blob/master/async%20&amp;%20performance/ch1.md#jobs">microtasks (aka Jobs)</a>. Basically, if each step in a promise chain resolves immediately, each subsequent step is scheduled "right away", such that no other waiting asynchronous operations get to run.</p>
  105. <p>Or what if you had ten sets of HTTP request/response cycles, but you need to make sure that no more than 3 of them were being processed at any given moment? Or perhaps you needed to not proceed ASAP but rather in a strictly round-robin order through the set of running tasks.</p>
  106. <p>There's probably half a dozen other scenarios where naive scheduling of the progression of async operations is not sufficient. In all those cases, we'd like to have more control.</p>
  107. <p>An <code>async function</code> affords no such control because the promise-resumption step is baked into the engine. But a smarter <code>run(..)</code> utility driving your generators could absolutely selectively decide which ones to resume in which order.</p>
  108. <p>By the way, while I'm being general about such scenarios for brevity sake, this is not purely theoretical. An open area of my research in async programming is <a href="..">CSP</a>, Communicating Sequential Processes, the model for concurrency used in the <em>go</em> language and also in ClojureScript's <code>core.async</code>.</p>
  109. <p>Normally CSP processes are modeled in JS using generators, because you will in general have many of them running at the same time, and usually need fine grained control over which ones resume after others pause. Attempts have been made to model CSP using <code>async function</code>s, but I believe these systems are inherently flawed in that the library is susceptible to all the above limitations because it has no control over the scheduling.</p>
  110. <p>CSP is one very concrete example of why it's important to have scheduling control over async operations. <code>async function</code>s just don't offer what we need, but generators do.</p>
  111. <h3 id="async-functions-vs-generators"><code>async function</code>s vs Generators</h3>
  112. <p>OK, so let's take stock of where we're at.</p>
  113. <p>First we examined how <code>async function</code>s work and what makes them so nice from a syntactic sugar perspective. But now we've seen several places where the tradeoff is that we lose control of potentially very important aspects of our asynchrony.</p>
  114. <p>You may consider these limitations unimportant because you haven't needed to do any of the fine-grained control described here. And that's fine. The <code>async function</code> will serve you just fine.</p>
  115. <p>But... what pain are you <em>really</em> enduring if you stuck with generators? Again, a side-by-side comparison:</p>
  116. <pre class="code">
  117. function \*foo(x) {
  118. try {
  119. var y = yield getY( x );
  120. return x + y;
  121. }
  122. catch (err) {
  123. return x;
  124. }
  125. }
  126. run( foo( 3 ) )
  127. .then( function(result){
  128. console.log( result );
  129. } );
  130. </pre>
  131. <p>versus:</p>
  132. <pre class="code">
  133. async function foo(x) {
  134. try {
  135. var y = await getY( x );
  136. return x + y;
  137. }
  138. catch (err) {
  139. return x;
  140. }
  141. }
  142. foo( 3 )
  143. .then( function(result){
  144. console.log( result );
  145. } );
  146. </pre>
  147. <p>They're almost identical.</p>
  148. <p>In both versions you get localized synchronous semantics (including pause/resume and error handling), and you get a promise that represents the completion of the <code>foo(..)</code> task.</p>
  149. <p>For generators, you <em>do</em> need the simple <code>run(..)</code> library utility, whereas with <code>async function</code>s, you don't. But as we discussed, such a utility is only about a dozen lines of code in its simplest form. So is it actually that burdensome?</p>
  150. <p>For the extra control we get, I think <code>run(..)</code> is an easy price to justify.</p>
  151. <h3 id="finally"><code>finally { .. }</code></h3>
  152. <p>For the majority of my async code, I believe generators+promises is a better, more flexible, more powerful option, at only a minimal expense.</p>
  153. <p>I'd rather not start with <code>async function</code>s and then later realize that there are places where I need more control, and have to refactor back to generators. That's especially true considering the <a href="https://blog.getify.com/not-awaiting-1/#just-use-both">complications of mixing the two forms</a> that I addressed in part one.</p>
  154. <p><code>async..await</code> seems like a great feature. But it's too simplistic, IMO. The small syntactic benefits are not worth the loss of control.</p>