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

Trying to save a functions data to a CSV file

So this is my JS code

getAllShirtDetailsAsync()
    .then(function(data) {
        console.log(data);
    })
    .catch(function(err) {
        console.log(err);
    });


var shirtInfo = getAllShirtDetailsAsync();

fs.mkdir('data',function(){
    fs.writeFile('./data/t-shirtdata.csv', JSON.stringify(shirtInfo))


})

The getAllShirtDetailsAsync() gets details of a 8 different shirts in an array of objects, I then want to spit this array of objects containing the info into readable data in an CSV file. So I create a directory with fs.mkdir called data, then create a csv file within that directory, I then try to stringify the array of information with the variable called shirtInfo which relates to the function.

When I go into the data csv file all that comes up is "{}" an non of the data. Anybody know why this is?

2 Answers

getAllShirtDetailsAsync() is an async method, which means you can't do this:

var shirtInfo = getAllShirtDetailsAsync();

You need to do something like this:

getAllShirtDetailsAsync()
    .then(function(data) {
        //Do the csv-code here. 
    })
    .catch(function(err) {
        console.log(err);
    });

or even structure it like this:

getAllShirtDetailsAsync()
    .then(writeToCsv)
    .catch(handleErr);


function writeToCsv(data) {
    //write to csv
}

function handleErr(err) {
    //error handling here
}

I still urge you to read about callbacks and promises, in order to get a better understanding of this.

I don't care if you used my code. What's important is that you learn from it and actually understand it :)

Don't worry, I have been over several tutorials about promises and understood the concept for the most part, I can't actually just use something without understanding it, there would literally be no point, I want to learn rather than just use code in order to pass.

Although going through my understandings of promises, does a promise always return the value asynchronously? So it happens as soon as it able to do so and does not wait in line?

So the reason I can not save the asynchronous function is because the function may not have been processed and retrieved yet? Is this why you can't do it like this? Therefore the "then" keyword will act as the next step when the asynchronous data has finished? So it's saying "then do this". Please correct me if my understanding is wrong at any point here. Thank you Thomas so far, you've been a big help.

And what exactly is the different between a promise which is called and using a "Async function" that was introduced in ECMAScript 2017? Are they similar in nature?