2016-09-25 120 views
1

我正在Node js Express中創建一個函數,客戶端會調用它來下載內容。從Promise返回Promise.All

內容需要從不同的來源下載,並且只有當所有下載完成(節點下載的內容被壓縮併發送回呼叫客戶端)時才需要發送迴應。因此,所有的下載功能都包裹在Promise.all(download1(),download2(),download3())

其中一個下載功能不僅下載內容,而且還生成一個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' 
     } 
} 

這是函數如何下載1貌似

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。 download1函數在Promise.all()中調用。一旦download1,download2和donwload3完成'.then'被執行。我無法找到一種方法來獲取由downloadWall內的donwload1返回的數據(statusJson)。

+0

這是什麼'的console.log(結果)'做,做它不包含你正在尋找的數據? – Bergi

回答

1

我無法找到一種方法來獲取由downloadWall內的donwload1返回的數據(statusJson)。

這是數組中的第一項,您在您的then回調接受作爲resultPromise.all()

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後失蹤()

例子:

// (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); 
 
    }); 
 
    }; 
 
}