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.
You want to use
RSVP.all()
:Feel free to make
promises
an object and useRSVP.hash()
instead:(thanks to @Benjamin Gruenbaum for suggestion to use
.map()
)