As I know that
'Any function that is passed as an argument is called a callback function'
, and also that if the callback function takes some time to complete, the statements followed by this callback function will run. Here is example about using callback in array.filter()
from MDN:
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
My question is why I can get console.log(result)
output immediately, and instead of 'undefined' or something?
Does it mean the callback
used inside the Array.prototype.filter()
can compete very fast. Only those callbacks which take some time like download things, reading files, talking to database, etc. (readFile('example.text)
may have undefined
result value, shown in this example?