Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Asynchronous Programming with JavaScript Asynchronous JavaScript with Callbacks Implement a Callback

Hamzah Iqbal
seal-mask
.a{fill-rule:evenodd;}techdegree
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 Points

How does the generateHTML work, when no functions are passed to it?

function generateHTML(data) {
  const section = document.createElement('section');
  peopleList.appendChild(section);
  section.innerHTML = `
    <img src=${data.thumbnail.source}>
    <h2>${data.title}</h2>
    <p>${data.description}</p>
    <p>${data.extract}</p>
  `;
}

btn.addEventListener("click", () => {

getJSON(astrosUrl, (json) =>

{

 person.people.map(person=> {

   getJSON(wikiUrl + person.name, generateHTML);

 });

I can somewhat see the reason for the generateHTML, however I cant see, why no data is passed in as the argument here. How is the data passed in here?

1 Answer

Steven Parker
Steven Parker
210,147 Points

In the "getJSON" call shown here, only a reference to "generateHTML" is being passed as a callback. It's not being called yet.

But if you go look at the definition for "getJSON" (not shown here), you can see that when it does get called (at line 13 of "callback.js"),, it is passed a data argument ("callback(data)").