The assignment I'm working on is to design a Node.js program that presents the user with command line prompts and writes a well-organized README markdown file with the input. The prompts are created with the Inquirer NPM. The code starts with the Inquirer prompts, then one or multiple .then statements create the file using FS's "writeFile" and "appendFile," and add elements in a particular order. For some reason, no matter what I do, I can't seem to get my code to add these elements to the new file in the proper order. I originally had everything under one .then statement, but then I figured the bug would be fixed if I gave each item to append its own ".then" and chained them together. But the output still comes out wrong -- in fact it seems to end up coming out in a different random order each time. Here's a few of these .then statements -- the whole block is pretty long so hopefully this provides enough context. You can also imagine this appended to a series of Inquirer prompts.
.then((response) => {
fs.writeFile('./output/README.md', '', (err) => err ? console.log(err) : null);
return response;
})
.then((response) => {
if(response.title) {
fs.appendFile('./output/README.md', `# ${response.title}\n`, (err) => err ? console.log(err) : null);
} else {
console.log('ALERT: No title provided.');
}
return response;
})
.then((response) => {
if(response.license) {
for(const i of licenses) {
if(i.name == response.license) {
fs.appendFile('./output/README.md', `${i.badge}\n`, (err) => err ? console.log(err) : null);
}
}
}
return response;
})
.then((response) => {
if(response.desc) {
fs.appendFile('./output/README.md', `## Description\n${response.desc}\n`, (err) => err ? console.log(err) : null);
} else {
console.log('ALERT: No description provided.');
}
return response;
})
TL;DR I tried chaining some .then statements to get this code to write a markdown file with a particular ordered structure, but it's still executing in an incorrect, seemingly random order.