Callbacks in JavaScript

0

I would like to understand something about callbacks! I know there are other topics on the subject, but mine is a simple question that is not clear to me yet.

For example, let's say I have this array:

['Banana', 'Maçã', 'Melancia']

To go through the same I'm going to use forEach with a function and I'll display with console.log

const minhaArray = ['Banana', 'Maçã', 'Melancia'];

function impri(nome, indice){
  console.log('${indice + 1}. ${nome}');
}

minhaArray.forEach(impri);

The callback in the case, is it only the impri function or the callback all together? The function printed from the array and the forEach?

    
asked by anonymous 04.07.2018 / 14:59

2 answers

3

Callback or Callback function is the function that is passed to another function, and which will be called later when needed.

Confirm the Callback function definition in MDN

So in your example, callback is the impri function, since it is passed to forEach .

    
04.07.2018 / 15:16
3

The callback concept is more common in asynchronous cases. It is possible to say that impri is callback, although the concept itself is more common in asynchronous processes, because it is the function that is passed as argument and will be called back.

In this case, to answer your question: impri is the callback, the function itself and not the set where it is used.

    
04.07.2018 / 15:16