parseResponse = (response, cb) ->
output = ''
response.setEncoding('utf8')
response.on 'data', (chunk) -> output += chunk
response.on 'end', ->
j = JSON.parse(output)
result = j.results[0]
cb(result)
I’m trying to understand what this is doing and why it is necessary in a module I’m using. The response being passed in is from an http get.
Thanks
This function processes a response (instance of the
ClientResponseclass) being received via HTTP.Indicates the desired transfer encoding (UTF-8). Chunks passed to the
dataevent will be sent in this encoding.Sets up a callback for processing data chunks. Each “chunk” of a string received is appended to the
outputstring variable.Sets another callback which acts on the completely transferred data.
The received data is assumed to be JSON and parsed as such. The first element of the parsed array is retrieved.
The callback
cboriginally provided to the function is called with this data found in the JSON object.