Wednesday 22 March 2017

Question regarding the asynchronous nature of nodejs.

Hello, im fairly new to nodejs and learning about the asynchronous nature of node by using coffeescript. in node, every http call made is asynchronous , im making these calls in a for loop of 5, but why do i see the value of counter as 6 inside the call when made.? shouldnt it display value 5 ?------> for i in [1..5] ....... console.log "counter is #{i}" ....... http.get { host: 'www.google.com' }, (res) -> ....... console.log res.statusCode ....... console.log "counter is #{i}" ....... console.log "counter is #{i}" counter is 1 counter is 1 counter is 2 counter is 2 counter is 3 counter is 3 counter is 4 counter is 4 counter is 5 counter is 5 [ undefined, undefined, undefined, undefined, undefined ] coffee> 200 counter is 6 200 counter is 6 200 counter is 6 200 counter is 6 200 counter is 6 undefined

Submitted March 22, 2017 at 08:17AM by russotiquerra

No comments:

Post a Comment