Need clarification of callbacks in this challenge

Need clarification of callbacks in this challenge
0

#1

When we get to the following section:

Functional Programming: Implement map on a Prototype

people seem to be struggling far more than necessary just by looking at forum posts in regards to the challenge. FreeCodeCamp needs to be more up front stating

You don’t have to use the word “callback” as the name of your argument, Javascript just needs to know that it’s the correct argument name.

because I thought callback was this whole new thing I had to understand ( almost like a keyword or it had some code it was invoking behind the scene), when in reality, after much frustration and toil, I find it is just a naming convention some people feel is necessary to highlight what is going on. The truth is if you have a solid understanding of passing arguments to functions and keeping your references right, then the need for the callback naming convention becomes irrelevant to me. Looking on the internet, this word seems to cause entirely too much confusion!

I saw SO many people on the forums being puzzled by callback and I have to wonder if their reason is related to mine.

I ended up intentionally writing my solution to the challenge I referenced above as:

var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callFromMultiplyByTwoFunction){
  var newArray = [];
  for(let i = 0; i < s.length; i++)
  {
    newArray.push(callFromMultiplyByTwoFunction(this[i]));
  }
  return newArray;

};
 
var new_s = s.myMap(function(item){
  return item * 2;
});

The fact that you don’t have to use this callback naming convention is buried in the following post:

https://www.freecodecamp.org/forum/t/javascript-callback-functions/14658

I think people could learn what they need to more effectively and efficiently if we cleared up the instructions and information presented, just my two cents.


#2

Yeah, callback has an air of mystery surrounding it. I think “callback” is kind of a clue word that suggests “future user can insert their own function here” or “function yet to be determined”. That way any callback function passed that meets the parameter requirements should work as expected even if the callback was written by a different programmer years later.

console.log(s.myMap(double));
console.log(s.myMap(triple));
console.log(s.myMap(half));
console.log(s.myMap(square));

Since the author of the method probably won’t have prior knowledge of the object(s) it is getting called on you need to avoid any reference to a specific object and instead refer to an object in a generic way.

For instance because of this expression i < s.length; this particular method would only work if there was an array named ‘s’ in scope when executed. i < this.length would be a more general way to refer to the array method is called on.


#3

Actually, a callback should be taken quite litteral. “Call me back”. It’s usually used on asynchronous functions. In the simplest explanation:

When you are done with this, call me back to perform this function. The function being the function passed to the callback parameter.

One of the best examples would be the JS timeout function

setTimeout(function(){
	alert("Hello World");
}, 10000);

Although it doesn’t inmediately show, this is an asynchronous callback function. It says: Call me back after 10 seconds to perform the function with the alert in it.

Javascript will move on to do other stuff. If you were to put an alert right underneath that timeout function, it would trigger straight away. Even though the timeout function isn’t “finished” yet. That’s why it’s called a callback function. It calls upon Javascript to come back towards the code when needed.

This is particularly usefull in Ajax functions. It sends data to the server (or requests data from a server) and moves on, telling it to call JS again once the data (or an error) has arrived, so it can do something with that data. If JS was forced to wait for it instead, the whole browser would freeze untill the data arived. Imagine the server being offline. That means a browser freeze that doesn’t solve itself.

On topic: This is also the reason why people use the word callback even if it isn’t nessesary. If some other programmer were to look at your code and sees the word callback, (s)he immediately understands that there will be a callback function passed towards that argument. Whereas if you were to use foo, another programmer would have to dive deeper into your code in order to understand what “foo” would contain.

Naming conventions (or other conventions) aren’t just there because some ppl like it. They serve a purpose. And not just to make your code readable for other programmers, but for you as well. Right now you might be developing simple websites or apps with just a few lines of code. But once you get there, you’ll be developing apps with thousends of lines of code. If you don’t stick to certain conventions, and have to come back to your code a couple of months later for some reason, it’s gonna take you a lot of time to figure out how everything was working again if your code doesn’t give you any obvious clues.


#4

Thanks @alhazen1 and @bjorno43,

I was just under the impression that is why I always need to have meaningful names for my variables and references. I see what you are saying for certain applications. Thanks for your thorough response!