Q:

How to using callback function to show all month using map method javascript

I am beginner at javascript. I want to understand more Callback Function because i have spend a lot time to learning this function but i not yet understood clearly.
This is my code. i want to make new function(callback) to continue this code, and show all month using method map
const getmonth = (callback) => {
    setTimeout(() => {
        let error = true;
        let month = ["January","February","March","April","Mey","Juny","July","August","September","October","November","Desember"];
        if(!error) {
            callback(null, month)
        } else {
            callback(new error("Data gak ketemu", []))
        }
    }, 4000)
}

A:

Callback is the function, which is passed to another function as an argument.
For example:
function sayHello(callback) {
  console.log('Hi everyone');
  setTimeout(function(){
    callback(); // execution your function with 3 seconds delay
  }, 3000);
}

In your case(I'm not using arrow functions to make it more easy to understand for you):
// Lets create a function, which will just print month which is passed as an argument
const printMonth = function(month) {
  console.log(month);
}

// Now we are using map function
// https://developer.mozilla.org/uk/docs/Web/JavaScript/Reference/Global_Objects/Array/map

month.map(function(month) {
  console.log(month); 
});

The map function accepts another function as an argument(it's called callback) and in this function you can do whatever you want with every single element of array. You can also use an return to return new array with modified elements of it.

