Where Do the Parameters in a JavaScript Callback Function Come From

Where do the parameters in a javascript callback function come from?

They come from the same place they come from when a normal non callback function is invoked, at invocation time.

If you have this function,

function add (a, b) {
return a + b
}

You're fine with knowing that a and b come from when you invoke add,

add(1,2)

and it's the same principle with callbacks, don't let your brain get all twisted just because it's getting invoked later.

At some point the function you pass to router.get is going to be invoked, and when it does, it will receive req and res.

Let's pretend the definition for router.get looks like this

router.get = function(endpoint, cb){
//do something
var request = {}
var response = {}
cb(request, response) // invocation time
}

In the case of your example, it's just up to node to pass your function request and response whenever .get is invoked.

JavaScript: Passing parameters to a callback function

If you want something slightly more general, you can use the arguments variable like so:

function tryMe(param1, param2) {
alert(param1 + " and " + param2);
}

function callbackTester(callback) {
callback(arguments[1], arguments[2]);
}

callbackTester(tryMe, "hello", "goodbye");

How arguments are passed in callback functions?

They get passed in because forEach has some code which passes them in. forEach's implementation will look something like this:

forEach(callback) {
// `this` is the array we're looping over
for (let i = 0; i < this.length; i++) {
callback(this[i], i, this);
}
}

So forEach handles the logic of looping over the array, and then for every element in the array, it's going to call your function and pass in three parameters (the value, its index, and the entire array). You just write a function that uses those values in whatever way you need it to. If you don't need to use all 3 parameters, you can simply leave out any arguments to the right of the ones you need.

Where do the promise callback arguments come from

The promise constructor behaves like this:

 function Promise(callback){
const handlers = [];

function resolve(value){
for(const [success] of handlers)
if(success) success(value);
}

function reject(error){
for(const [, catch] of handlers)
if(catch) catch(error);
}

// Now call the callback and pass the functions:
callback(resolve, reject);

// Return the created promise...
}

Where does the error parameter from the first argument of a callback function come from?

I think you're talking about where do error and result come from.

This is a standard js callback style. error will be null if no insertOne executed successfully, and will be an error if something went wrong.

For example, this is kind of insertOne

const insertOne = (params, callback) => {
try {
// Do insert logic and set result to 'result' variable
callback(null, result)
} catch (error) {
callback(error, null)
}
}

Callback function parameters

The answer is that newUser.save also takes a callback parameter and passes it the same kind of parameters. So you can just pass the callback straight into save. I imagine user.save would look something like the following:

User.prototype.save = function(callback) {
//do stuff to save the user
//maybe get an error in the process, or a user record, pass them to the callback
callback(saveError, userRecord)
}

Because the expected args for save and addUser are the same, the callback can be passed straight into save.

Edit:
I would however suggest to invoke your callback with the error if one is returned from the bcrypt call. Since you already have a callback to give the error to, it doesn't make much sense to throw. Callers will expect errors to come in the callback, so I suggest this instead:

bcrypt.hash(newUser.password, salt, (err, hash) => {
if(err) {
callback(err, null)
return
}
...


Related Topics



Leave a reply



Submit