0

Is it possible to get data of two files like so using then function using bluebird promise?

fs.readFileAsync('directory/file1.txt')
        .then(function(fileData1){
            return fs.readFileAsync('directory/file2.txt');
        })
        .then(function(fileData2){
            return console.log(fileData1+fileData2);
        })
M.Dagiya
  • 203
  • 3
  • 13

1 Answers1

1

You can just wrap the first fs.readFileAsync('directory/file1.txt') call in Bluebird.resolve to convert to Bluebird promises, but you won't have the result of the first file in the handler of your second 'then' function.

In your case you can do them both in parallel using Bluebird.all.

Bluebird.all([
  fs.readFileAsync('directory/file1.txt')
, fs.readFileAsync('directory/file2.txt')
])
  .spread(function(file1, file2) {
    console.log(file1 + file2);
  })

If you use Bluebird.all remember that it takes an Array.

Jason Rice
  • 1,686
  • 1
  • 12
  • 17