Search code examples
javascriptnode.jspromisersvp.jsrsvp-promise

How to read multiple files asynchronously with promises, then proceed


I'm new to promises and using the rsvp implementation.

I want to asynchronously read a list of files, then proceed to another task only when all files have been read.

I've got as far as the basic structure to read one file, and chain to the next task:

var loadFile = function (path) {
    return new rsvp.Promise(function (resolve, reject) {
        fs.readFile (path, 'utf8', function (error, data) {
            if (error) {
                reject(error);
            }
            resolve(data);
        });
    });
};

loadFile('src/index.txt').then(function (data) {
    console.log(data);
    return nextTask(data);
}).then(function (output) {
    //do something with output
}).catch(function (error) {
    console.log(error);
});

I want to do something like this:

loadFile(['src/index.txt', 'src/extra.txt', 'src/another.txt']).then( ...

I've seen arrays of promises and hash of promises in the docs, but I don't know which is most relevant, or how to use them. I need an example of their use in the context of my problem above to understand them.


Solution

  • You want to use RSVP.all():

    var promises = ['path1', 'path2', 'path3'].map(loadFile);
    
    RSVP.all(promises).then(function(files) {
      // proceed - files is array of your files in the order specified above.
    }).catch(function(reason) {
      console.log(reason); // something went wrong...
    });
    

    Feel free to make promises an object and use RSVP.hash() instead:

    var promises = {
      file1: loadFile('path1'),
      file2: loadFile('path2'),
      file3: loadFile('path3')
    };
    
    RSVP.hash(promises).then(function(files) {
      // files is an object with files under corresponding keys:
      // ('file1', 'file2', 'file3')
    }).catch(function(reason) {
      console.log(reason); // something went wrong...
    });
    

    (thanks to @Benjamin Gruenbaum for suggestion to use .map())