javascript - How to read multiple files asynchronously with promises, then proceed - Stack Overflow

I'm new to promises and using the rsvp implementation.I want to asynchronously read a list of file

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.

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.

Share Improve this question asked Feb 22, 2015 at 19:36 mtmacdonaldmtmacdonald 15.1k20 gold badges69 silver badges102 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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())

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745335538a4623085.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信