Eddie Lam
Eddie Lam

Reputation: 619

Calling a function vs calling a function in a callback

I want to understand how the getJSON function is invoked differently in the following two situations. What is getJSON if it is not a callback function, when I don't wrap it inside an arrow function?

First:

btn.addEventListener("click", ()=> {
  getJSON(astroURL);
})

Second:

btn.addEventListener("click", getJSON(astroURL))

Upvotes: 0

Views: 27

Answers (1)

Unmitigated
Unmitigated

Reputation: 89204

The second argument to addEventListener is supposed to be the callback that is invoked each time the event occurs. Your first example is passing an arrow function as the callback, while your second example is passing the return value of getJSON(astroURL) to addEventListener, which is erroneous, unless the method itself returns a function.

Upvotes: 1

Related Questions