Search⌘ K
AI Features

Exercise: Recursive Find

Explore how to build a recursiveFind function in Node.js that locates text files containing a specific keyword within a directory and its subdirectories. This lesson guides you through managing asynchronous control flow with callbacks and executing searches in parallel while limiting concurrency to improve efficiency and reliability.

Problem statement

Write a recursiveFind() named callback-style function that takes a path to a directory in the local filesystem and a keyword, as per the following signature:

function recursiveFind(dir, keyword, cb) { /* ... */ }

The function should find all the text files within the given directory that contain the given keyword in the file contents. The list of matching files should be returned using the callback when the search is completed. If no matching file is found, the callback should be invoked with an empty array. As an example test case, if you have the files foo.txt, bar.txt, and baz.txt in myDir and the keyword batman is contained in the files foo.txt and baz.txt, you should be able to run ...