Closed jamiewilson closed 7 years ago
@jamiewilson You would be correct to assume that this code passes only when the HTTP requests happen to complete in the correct order. What's happening here is you are mixing asynchronous code with synchronous code (the for
loop). The synchronous code does not wait for the asynchronous code to finish before continuing.
One solution is to wrap each function call of httpGet()
in a Promise:
const theArguments = [] // All of the arguments passed into process.argv without [0] and [1]
Promise.all(
theArguments.map(arg => Promise.resolve(httpGet(arg)))
);
Here's what's happening. Promise.all()
takes an array of Promises. Each individual HTTP request is wrapped in a Promise, and then passed into Promise.all()
. This lets each promise resolve, and it returns an array of the resolve values from each promise in the order passed to Promise.all()
.
Somebody correct me if I'm wrong here.
@matthamil Your instruction was spot-on. Thank you. I am closing this issue because it think @jamiewilson managed to continue.
I'm stuck!
My solution sometimes passes verification, but only because—I suspect—the callbacks just happen to match the order in which each httpGet is able to finish. But even with studying the official solution, I've not been able to figure this one out.
Since the prompt suggests to try this without a helper library, it would be nice for the official solution to show that approach as well. Here is where I'm at. Any suggestion where I might be going wrong or ignoring something would be super helpful!