marijnh / Eloquent-JavaScript

The sources for the Eloquent JavaScript book
https://eloquentjavascript.net
3.02k stars 796 forks source link

Ch 11, Network flooding example #493

Closed jasontwuk closed 5 years ago

jasontwuk commented 5 years ago

Hi, I think the example's outcome in this section is a bit strange. https://eloquentjavascript.net/11_async.html#p_2KFj5q8DUa

routeRequest(bigOak, "Church Tower", "note",
           "Incoming jackdaws!");

If you use the "Run code" function to run the code, it will print out the result several times (one time, two times or sometimes three times). Is it supposed to print its result more once?

Also, I have tried to run this code in Node.js, it threw the following error message:

Error: No route to Church Tower

But if you change the target in routeRequest function from "Church Tower" to "Cow Pasture", "Butcher Shop" or "Gilles' Garden", the code works fine. It seems like it only can pass the message to its next door neighbours.

marijnh commented 5 years ago

The crude implementation of networking in the code doesn't prevent duplicate messages, so when a request times out due to the simulated unpredictable network speed, the message might get sent again even though the original message is also still in flight. This is expected. I've adjusted the code a bit in attached patch so that this happens less often.

In node.js, you should give the network a moment to propagate routes before you try to run routeRequest (for example by wrapping your code in a setTimeout call).

jasontwuk commented 5 years ago

Thank you very much.