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.

Brandon Leichty
Full Stack JavaScript Techdegree Graduate 35,193 PointsCan't get Promise.all() to properly work
Hey all,
I'm trying to use Promise.all() to wait until an array of promises are resolved. Any JavaScript Promise pros out there? Here's a snippet of my code.
The .map method is properly working--executing over each array item in urlArray. But for some reason 'FINISHED!' is instantly logged to the terminal.
Maybe I'm not properly resolving in the scrapeShirtInformation() function?
Any thoughts would be very much appreciated. I've been trying to figure this out for day snow.
//more code above this
.then(function(urlArray){
const v = urlArray.map(fn)
Promise.all(v).then(console.log('FINISHED!'));
});
const fn = function scrapeShirtInformation(url){
let finish = false;
return new Promise((resolve, reject) => {
request(url, function (error, response, body) {
if (!error) {
const $ = cheerio.load(body);
let price = $('.price').text();
let title = $(".shirt-details h1").text().substr(price.length + 1);
let relativeImageUrl = $(".shirt-picture img").attr("src");
let imageUrl = rootURL + relativeImageUrl;
console.log('Writing data for: ' + url);
let shirtInfo = {};
shirtInfo.price = price;
shirtInfo.title = title;
shirtInfo.relativeImageUrl = relativeImageUrl;
shirtInfo.imageUrl = imageUrl;
//push shirt info into infoToWrite array
infoToWrite.push(shirtInfo);
console.log(infoToWrite);
resolve(shirtInfo);
} else {
reject(console.log('FAIL!'));
console.log(`An error was encountered: ${error.message}`);
}
});
})
}
1 Answer

Jose Soto
23,407 PointsTry this:
Promise.all(v).then(values => {
console.log("FINISHED");
});
Brandon Leichty
Full Stack JavaScript Techdegree Graduate 35,193 PointsBrandon Leichty
Full Stack JavaScript Techdegree Graduate 35,193 PointsWow. I can't believe that's all it was! You're my hero.
Can you elaborate one what makes your code work by add values? Thank you so much!
Jose Soto
23,407 PointsJose Soto
23,407 PointsGlad I could help. The contents of the
then
chain needs to be a function. You can read the official documentation here.