const fs = require('fs').promises
fs.readFile('a.txt', 'utf8')
.then(aContents => {
// If the contents of a.txt don't end with a newline character,
// reject with an error
if (!aContents.endsWith('\n')) {
throw new Error('Missing newline at end of a.txt')
}
// Otherwise, read b.txt and concatenate the files' contents
return fs.readFile('b.txt', 'utf8')
.then(bContents => Promise.resolve(aContents + bContents))
})
// If either file didn't exist or a.txt didn't end with a newline,
// resolve to the empty string instead
.catch(err => Promise.resolve(''))
// Finally, write the string to c.txt
.then(concatenated => fs.writeFile('c.txt', concatenated))
-
Caleb C. Sander authored53fbefe6