My libraries:

```
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('graceful-fs'));
const path = require('path');
const xml2js = Promise.promisifyAll(require('xml2js'));

```

I have a large number of XML files I want to parse. I am able to create an array of paths to all the files using this function:

```
function getFileNames(rootPath) {
  // Read content of path
  return fs.readdirAsync(rootPath)
    // Return all directories
    .then(function(content) {
      return content.filter(function(file) {
        return fs.statSync(path.join(rootPath, file)).isDirectory();
      });
    })
    // For every directory
    .map(function(directory) {
      // Save current path
      let currentPath = path.join(rootPath, directory);
      // Read files in the directory
      return fs.readdirAsync(currentPath)
        // Filter out the XMLs
        .filter(function(file) {
          return path.extname(file) === '.XML';
        })
        // Return path to file
        .map(function(file) {
          return path.join(rootPath, directory, file);
        });
    })
    // Flatten array of results
    .reduce(function(a, b) {
      return a.concat(b);
    });
}

```

and now I want to go trough every single file and parse it.

I have 2 function to do so:

```
function openFile(filePath) {
 return fs.readFileAsync('./' + filePath)
  .then(function(fileData) {
    return fileData;
  });
}

function parseFile(data) {
  return xml2js.parseStringAsync(data)
      .then(function(xmlObject) {
        return xmlObject;
      });
}

```

Now when I call this with the .map (the `GetFileNames` function outputs an array with over 20k strings with file paths) function:

```
getFileNames('./XML')
  .map(function(file) {
    openFile(file)
      .then(function(data) {
        parseFile(data)
            .then(function(object) {
              console.log(object);
            });
      });
  });

```

I get a javascript heap out of memory error:

> 
> FATAL ERROR: CALL\_AND\_RETRY\_LAST Allocation failed - JavaScript heap
>  out of memory
> 
> 
> 

But when I run the function a single time by passing in the path to the actual file:

```
openFile('./XML/2016-10-1/EUROTIPOLD2016-10-1T00-00-22.5756240530.XML')
  .then(function(data) {
    parseFile(data)
        .then(function(object) {
          console.log(object);
        });
  });

```

I get the desired output.

What am I doing wrong?