Reputation: 97
So, I'm learning about promises, and to demonstrate promises, the instructor is first starting with traditional callback example. However, I'm a little confused on the syntax he's using and where some extra parameters seem to have appeared from.
What I'm looking for is a dumbed down explanation to help me wrap my head around it. These are my specific questions:
In the first line of the code below, is 'callback' and argument? Is it the name of the function? Or is it something else?
On the third and fourth lines, I understand what we're passing in (either the error or the result), but I don't understand where those two parameters came from since we didn't define two parameters anywhere. How does the program know that's okay and not throw an error?
I don't need help with understanding when the function is called, though, because I understand that, it's just some of the stuff in defining the function that I'm confused on. I think it might be the arrow syntax that's throwing me off.
I've tried googling and rewatching the videos over and over again, but still can't grasp what's going on.
const doWorkCallback = (callback) => {
setTimeout(() => {
// callback('This is my error', undefined)
callback(undefined, [1, 4, 7])
}, 2000)
}
doWorkCallback((error, result) => {
if(error) {
return console.log(error)
}
console.log(result)
})
Upvotes: 0
Views: 76
Reputation: 138257
Is 'callback' an argument?
No, it is the name of the parameter (an argument is what you pass to a function, a parameter is the variable used to access that argument from within the function).
Is it the name of the function?
Kind of. You can use that Identifier to accesss the variable (/parameter) which holds whatever value you passed to the function as an argument. In your case, you pass a function to it ((error, result) => ...
), so callback
will refer to that function.
On the third and fourth lines, I understand what we're passing in (either the error or the result), but I don't understand where those two parameters came from since we didn't define two parameters anywhere.
You did define a function that takes two parameters ((error, result) => {
). But that does not matter: You can pass any number of arguments to a function, no matter how many parameters it has. If you pass more arguments than parameters, the arguments will go into nowhere:
alert("used", "ignored");
If it is the other way round, parameters that don't receive an argument are undefined
:
function one(arg1) { /* arg1 is undefined */ }
one();
I think it might be the arrow syntax that's throwing me off.
Here is a version just with regular functions:
function doWorkCallback(callback) {
setTimeout(function() {
// callback('This is my error', undefined);
callback(undefined, [1, 4, 7]);
}, 2000);
}
doWorkCallback(function (error, result) {
if(error) {
return console.log(error);
}
console.log(result);
})
You can read about arrow functions on Codeburst and MDN ...
Upvotes: 6
Reputation: 196
1- Yes, callback
is the argument of your doWorkCallback
function.
2- You are passing down a function, not two arguments. That function does receive too arguments which are error
and result
.
So, if you go to your doWorkCallback function you see that you will be calling wathever the callback is, undefined as error and [1, 4, 7]
as result every 2 seconds
Upvotes: 1