Soubriquet
Soubriquet

Reputation: 3350

Asynchronous string concatenation with Node.js

I have a function that generates filenames using the data and time. In the code below, I concatenate the date and time together in separate statements. If I run the code as so below, will the fileName be returned before either the date or time can be concatenated? If I return everything in one line, will that ensure the filename will be complete before being returned?

function generateFileName()
{
    var date = new Date();

    var fileName = "scoreboard-";
    //date
    fileName += date.getMonth()+'-'+date.getUTCDate()+'-'+date.getUTCFullYear()+'-';
    //time
    fileName += date.getHours()+'-'+date.getMinutes()+'-'+date.getSeconds();

    //return fileName;
}

Upvotes: 0

Views: 614

Answers (1)

lukewestby
lukewestby

Reputation: 1207

The Date constructor and all assignments are synchronous operations. When you return fileName it will be the full file name as you have constructed it.

Upvotes: 2

Related Questions