getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
179.37k stars 33.48k forks source link

"Async & Performance": ch4, Delegating Recursion example #1391

Closed anBertoli closed 5 years ago

anBertoli commented 5 years ago

Hi, I really appreciate the YDKJS series! This is a complementary to #476 and #434 issues. I've found a questionable explanation for the "delegating recursion" example. Points 6-7:

  1. That promise is yielded out, which comes back to the *foo(2) generator instance.
  2. The yield passes that promise back out to the foo(3) generator instance. Another yield passes the promise out to the bar() generator instance. And yet again another yield * passes the promise out to the run(..) utility, which will wait on that promise (for the first Ajax request) to proceed.

Following the run(...) utility logic and the example above I do not think it is correct to say that the promise is passed back to the "parents" generator istances before completion, because basically run(..) simply creates a new Promise waiting the fulfillment of request( "http://some.url/?v=" + val ) and then, after fulfillment, the fullfillment value is passed to the paused yield statement. Only now, the value is returned to the "parent" generator instance.

getify commented 5 years ago

As I stated in #434, this whole section will be rewritten in second edition.