Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Asynchronous Programming with JavaScript Understanding Promises From Callbacks to Promises

amarpreet singh saluja
amarpreet singh saluja
1,053 Points

I'm getting promise status as fullfilled wherein , in the video it says resolved can you please explain the same?

const astrosUrl = 'http://api.open-notify.org/astros.json'; const wikiUrl = 'https://en.wikipedia.org/api/rest_v1/page/summary/'; const peopleList = document.getElementById('people'); const btn = document.querySelector('button');

function getJSON(url) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.onload = () => { if (xhr.status === 200) { let data = JSON.parse(xhr.responseText); resolve(data); } else { reject(Error(xhr.statusText)); } }; xhr.onerror = () => reject(Error('A network error')); xhr.send(); });

}

// Generate the markup for each profile function generateHTML(data) { const section = document.createElement('section'); peopleList.appendChild(section); // Check if request returns a 'standard' page from Wiki if (data.type === 'standard') { section.innerHTML = <div class="img-div"> <img src=${data.thumbnail.source}> </div> <div class="content-div"> <h2>${data.title}</h2> <p>${data.description}</p> <p>${data.extract}</p> </div> ; } else { section.innerHTML = <div class="img-div"> <img src="img/profile.jpg" alt="ocean clouds seen from space"> </div> <div class="content-div"> <h2>${data.title}</h2> <p>Results unavailable for ${data.title}</p> ${data.extract_html} </div> ; } }

function getProfiles(json) { const profiles = json.people.map(person => { return getJSON(wikiUrl + person.name); }); return profiles; }

btn.addEventListener('click', (event) => { getJSON(astrosUrl) .then(getProfiles) .then(data => console.log(data)) .catch(err => console.log(err)) event.target.remove();

});

1 Answer

Steven Parker
Steven Parker
231,268 Points

In promises, "resolved" means that it is no longer pending but it doesn't indicate whether it was successful or not.

If successful, it is also said to be "fulfilled". The other possibility is that it was "rejected".