2016-09-25 8 views
1

私はノードjs Expressにコンテンツをダウンロードするためにクライアントから呼び出される関数を作成しています。プロミスからPromiseへの戻り値すべて

コンテンツはさまざまなソースからダウンロードする必要があり、すべてのダウンロードが完了したとき(ノードによってダウンロードされたコンテンツが圧縮され、呼び出し元のクライアントに返送された場合)にのみ返信する必要があります。したがって、すべてのダウンロード機能はPromise.all(download1()、download2()、download3())にラップされます。

ダウンロード機能の1つはコンテンツをダウンロードするだけでなく、jsonを生成して、メイン機能。 main関数はそれを応答ヘッダーとして設定します。

クライアントによって呼び出されたAPI関数は、これは私が戻ってクライアントにヘッダーを送信する方法を知っている

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

どのように見えるかdownload1機能です。この

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' 
     } 
} 

のように見えます。私の挑戦は、downloadAll関数でstatusJsonを取得する方法です。 download1関数はPromise.all()内から呼び出されます。 download1、download2、およびdonwload3が完了するとすぐに.thenが実行されます。私はdownloadAll内のdonwload1によって返されたデータ(statusJson)を取得する方法を見つけることができません。

+0

'はconsole.log(結果は)'何をするのか、ありませんあなたが探しているデータを含んでいませんか? – Bergi

答えて

1

downloadAll内でdonwload1から返されたデータ(statusJson)を取得する方法が見つかりません。

それはあなたがPromise.all()にあなたのthenコールバックでresultとして受信アレイ内の最初のエントリです:

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

関連する問題