将价值从Promise返回Promise.All

我正在Node js Express中创build一个函数,客户端会调用它来下载内容。

内容需要从不同的来源下载,只有当所有下载完成(节点下载的内容被压缩并发回到呼叫客户端)时才需要发送回应。 所以,所有的下载function都包装在Promise.all(download1(),download2(),download3())

其中一个下载function不仅下载内容,而且还生成一个json并将其发送回主函数。 主函数将其设置为响应头。

客户端调用的API函数看起来像这样

function downloadAll(request, response) { // Download content only after folders are created return createDirPromise.then(function (result) { var statusJson = ... // somehow get the status json from download1() so that it can be send to the client return Promise.all([download1(contentsJson), download2(contentsJson), download3(contentsJson)]); }) .then(function (result) { return new Promise(function (resolve, reject) { // Create zip. Here is the zip creation logic. // Once zip is created, send it to client as a buffer zipdir(zipFolderPath, function (err, buffer) { if (err) { console.log('Error while zipping!!! '+JSON.stringify(err)); return reject({ data: null, resp_status_code: 500, resp_status_message: JSON.stringify(err)}); } } console.log('Zipping successful'); return resolve( { data: buffer, resp_status_code: 200, resp_status_message: "Zip succeeded", headers: statusJson }); }) }) .catch(function (error) { return { data: 'Error in any of above ' + error, resp_status_code: 500, resp_status_message: 'Error while zipping' } } 

这是download1函数的样子

 function download1(contentsJson) { return new Promise(function(resolve, reject) { //set the status json here var statusJson = { "key1": "value1", "key2": "value2"}; //do the download stuff here and return the statusJson console.log('Downloading complete, return the status so that it can be send to the client'); resolve(statusJson); } 

我知道如何将标题发送回客户端。 我的挑战是如何获得downloadAll函数中的statusJson。 从Promise.all()中调用download1函数。 一旦download1,download2和donwload3完成'.then'被执行。 我无法find一种方法来获取由downloadWall内的donwload1返回的数据(statusJson)。

我无法find一种方法来获取由downloadWall内的donwload1返回的数据(statusJson)。

这是您在Promise.all()callback中收到的数组中的第一个条目:

 function downloadAll(request, response) { return createDirPromise.then(function (result) { return Promise.all([download1(contentsJson), download2(contentsJson), download3(contentsJson)]); }) .then(function (result) // **** Here, the result of download1 is result[0] var statusJson = result[0]; 

Promise.all将所有诺言结果汇集在一起​​,并按照你所承诺的顺序将它们返回给一个数组。

(注意:我认为你在createDirPromise后缺less() 。)

例:

 // (Requires Promise support in your browser) "use strict"; var createDirPromise = createPromiseFunction("createDirPromise"); var download1 = createPromiseFunction("download1"); var download2 = createPromiseFunction("download2"); var download3 = createPromiseFunction("download3"); function downloadAll(request, response) { return createDirPromise().then(function(result) { return Promise.all([download1(/*contentsJson*/), download2(/*contentsJson*/), download3(/*contentsJson*/)]); }) .then(function(result) { // **** Here, the result of download1 is result[0] var statusJson = result[0]; console.log("statusJson = '" + statusJson + "'"); }); } downloadAll(); function createPromiseFunction(name) { return function() { return new Promise(function(resolve) { setTimeout(function() { console.log("resolving " + name); resolve("result of " + name); }, Math.random() * 50); }); }; }