1

I asked whats the equivalent of jquery when in angular and now I want to do similar thing in node. I need to something like this:

when(fs.readFile('file1'), fs.readFile('file2')) .done(function( a1, a2 ) { ... // do stuff });

How can I achieve this? Thanks.

Community
  • 1
  • 1
stevemao
  • 1,423
  • 1
  • 16
  • 29

1 Answers1

2

You need node 0.11.13 or higher for this to work or to use a library but that would be Promise.all:

var Promise = require("bluebird"); // Imports the Bluebird promise library in 
                                   // new versions of node this is optional

Promise.promisifyAll(fs); // this is required if using Bluebird, you'll have to 
                          // do this manually with native promises.
Promise.all([fs.readFileAsync('file1'), fs.readFileAsync('file2')])
.spread(function( a1, a2 ) {
   ... // do stuff
});

As for how to convert a callback API to promises - see this question and answer.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504