How does the callback in an http request work? Why is there a second callback? I thought the callback supplied with the request would be called when the response was returned, yet this seems to be placing another callback waiting on... something...?
When does res.on() get called?
Does res.on('data') get called only once per request, or can it be called multiple times, each time with a piece of the data? If the latter is true, how do I know that i've got all the pieces?
The use case I've got in mind is - I want to fetch a file and evaluate it, and am wondering if I can safely do this, or whether sometimes the data won't be complete, hence the eval will fail.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
How does the callback in an http request work? Why is there a second callback? I thought the callback supplied with the request would be called when the response was returned, yet this seems to be placing another callback waiting on... something...?
When does res.on() get called?
Does res.on('data') get called only once per request, or can it be called multiple times, each time with a piece of the data? If the latter is true, how do I know that i've got all the pieces?
The link in the http docs for more info on the node.js http api is dead - possibly when working this would explain it - but nodemanual.org seems to be down. http://nodemanual.org/latest/nodejs_ref_guide/http.html
The use case I've got in mind is - I want to fetch a file and evaluate it, and am wondering if I can safely do this, or whether sometimes the data won't be complete, hence the eval will fail.