-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
exercise 9 Juggling Async #467
Comments
The point of this exercise is to understand that By async work, I mean that the call to You avoid this by making a copy of the value: for (var i = 0; i < 10; i++) {
(function(index) {
// index is a copy of "i"
})(i);
} You need not copy anything else because the references are not changing while you are iterating. I struggled with this as well, see #287. My solution is also available as a gist. More information available from the no-loop-func eslint rule, on mdn, and stack overflow. You should also take the time to learn about |
Thank you for the links. I will go through them now. Will close if I don't have any further questions. |
Looks like you don't have any further questions :P @thekindlyone |
actually we could use this way to resolve this problem const http = require('http')
const url = process.argv.slice(2)
let resultQueue = []
let counter = 0
for(let i=0;i<url.length;i++){
http.get(url[i], function(response){
let result = ""
response.setEncoding("utf8")
response.on("data", function(chunk){
result += chunk
})
response.on("end", function(){
resultQueue[i] = result
counter ++
if(counter == url.length){
resultQueue.forEach(function(item){
console.log(item)
})
}
})
})
} I think it's more easy to understand than before |
@MLuka I agree removing the For the sake of defensive coding, you should probably still use an IIFE on diverse teams. You don't want a teammate changing that |
@CodeMan99 Thank you for your reminding. I'm still student. Never worked with anyone else.Your words help me a lot! |
After trying a lot of different things, this worked, but I don't fully understand why.
Why do I have to pass
i
to the higher order function that returns the callback forhttp.get
but notout
anddone
.done
will also be changed asynchronously by the callbacks of response.pipe, and so willout
. I feel like I am only partly getting the hang of this async functional stuff. HELP!The text was updated successfully, but these errors were encountered: