Search⌘ K
AI Features

Exercise: File Concatenation

Explore how to create a Node.js callback function that concatenates content from multiple source files into a single destination file in the correct order. This lesson helps you practice asynchronous programming by managing file reads and writes sequentially using callbacks.

Problem statement

Write the implementation of a concatFiles() named callback-style function that takes two or more paths to text files in the filesystem and a destination file.

function concatFiles (srcFile1, srcFile2, srcFile3, ... ,
dest, cb) {
// ...
}

This function should copy the contents of every source file into the destination file, respecting the order of the files, as provided by the arguments list.
For instance, given two files, if the first file contains foo and the second file contains bar, the function should write foobar (and not barfoo) in the ...