Ad

Node.js Callback Error And Result Clarification

- 1 answer

I have a question regarding Node.js callbacks. Specifically, do ALL node.js callbacks have (err, res) as parameters? I get confused because sometimes I see functions that have (req, res) as parameters and I am unsure of which to use... Any documentation on this would be very helpful...

Thank you.

Ad

Answer

Parameters that are accepted by callback function depend completely on callback function 'design'. From your post I would make assumption that you are dealing with http related methods, thats why you see (err, req, res). If you look at fs.readFile('text.txt', function (err, data) { ... }), parameters are different (you can call them whatever you want by the way).

I'll try to explain it on example of custom build callback function:

function useAsCallback(string){
  console.log("callback function says: " + string)
}

function main(someString, callback){
  callback(someString)
}

main('What a great callback', useAsCallback)
Ad
source: stackoverflow.com
Ad