javascript - Trouble understanding Node.js callbacks -
today first foray nodejs , particularly stumped trying understand way following piece of logic flows. logic follows:
request({ uri: db.createdbquery('identifier:abcd1234') }, function(err, response, body) { response.should.have.status(200); var search = json.parse(body); search.response.numfound.should.equal(1); done(); }); }); at higher level understand http request being made , function being called @ juncture taking response , doing it. trying understand proper order of calls , how binding of variables take place in above given logic. how compiler know how bind return values request anonymous function? basically, want gain understanding on how things work under hood snippet.
thanks
your question isnt specific node.js, feature of javascript.
basically calling request() defined function request(obj, callback)
internally, http request being called, , once completed, calls callback function pointer.
function request(obj, callback){ //http request logic... var err = request_logic_internal_function(); var response = ... var body = ... callback(err, response, body) } your code can restructured :
var options = { uri: db.createdbquery('identifier:abcd1234') }; var request_callback = function(err, response, body) { response.should.have.status(200); var search = json.parse(body); search.response.numfound.should.equal(1); done(); }; request(options, request_callback); what you're doing sending in function pointer variable.
Comments
Post a Comment