简体   繁体   English

JavaScript 中的 Promise.all:如何获取所有 Promise 的解析值?

[英]Promise.all in JavaScript: How to get resolve value for all promises?

I wrote the following node.js file:我编写了以下 node.js 文件:

var csv = require('csv-parser');
var fs = require('fs')
var Promise = require('bluebird');
var filename = "devices.csv";
var devices;

Promise.all(read_csv_file("devices.csv"), read_csv_file("bugs.csv")).then(function(result) {
    console.log(result);
});


function read_csv_file(filename) {
    return new Promise(function (resolve, reject) {
            var result = []
            fs.createReadStream(filename)
                .pipe(csv())
                .on('data', function (data) {
                    result.push(data)
                }).on('end', function () {
                resolve(result);
            });
    })
}

As you can see, I use Promise.all in order to wait for both operations of reading the csv files.如您所见,我使用Promise.all来等待读取 csv 文件的两个操作。 I don't understand why but when I run the code the line 'console.log(result)' is not committed.我不明白为什么,但是当我运行代码时, 'console.log(result)'没有提交。

My second question is I want that the callback function of Promise.all.then() accepts two different variables, while each one of them is the result of the relevant promise.我的第二个问题是我希望Promise.all.then()的回调函数接受两个不同的变量,而每个变量都是相关承诺的结果。

First question第一个问题

Promise.all takes an array of promises Promise.all接受一系列承诺

Change:改变:

Promise.all(read_csv_file("devices.csv"), read_csv_file("bugs.csv"))

to (add [] around arguments) to(在参数周围添加[]

Promise.all([read_csv_file("devices.csv"), read_csv_file("bugs.csv")])
// ---------^-------------------------------------------------------^

Second question第二个问题

The Promise.all resolves with an array of results for each of the promises you passed into it. Promise.all使用您传递给它的每个承诺的结果数组进行解析。

This means you can extract the results into variables like:这意味着您可以将结果提取到变量中,例如:

Promise.all([read_csv_file("devices.csv"), read_csv_file("bugs.csv")])
  .then(function(results) {
    var first = results[0];  // contents of the first csv file
    var second = results[1]; // contents of the second csv file
  });

You can use ES6+ destructuring to further simplify the code:您可以使用 ES6+ 解构来进一步简化代码:

Promise.all([read_csv_file("devices.csv"), read_csv_file("bugs.csv")])
  .then(function([first, second]) {

  });

Answer to your second question:回答你的第二个问题:

If you want the then callback to accept two different arguemnts, then you can use Bluebird and its spread method.如果您希望then回调接受两个不同的参数,那么您可以使用Bluebird及其spread方法。 See:看:

Instead of .then(function (array) { ... }) and having to access array[0] and array[1] inside of your then handler you will be able to use spread(function (value1, value2) { ... }) and have both variables named as you want.相反的.then(function (array) { ... })并具有访问array[0]array[1]你的内部then处理程序,你将能够使用spread(function (value1, value2) { ... })并根据需要命名两个变量。

This is a feature of Bluebird, it's not possible with plain Promise .这是 Bluebird 的一个特性,普通的Promise不可能的。

You use Bluebird just like Promise, eg:您可以像使用 Promise 一样使用 Bluebird,例如:

var P = require('bluebird');
// and in your code:
return new P(function (resolve, reject) { ...
// instead of:
return new Promise(function (resolve, reject) { ...

Of course you don't have to name it P but whatever you want.当然,您不必将其命名为P但您可以随意命名。

For more examples see the Bluebird Cheatsheets .有关更多示例,请参阅Bluebird Cheatsheets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM