私はノード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)を取得する方法を見つけることができません。
'はconsole.log(結果は)'何をするのか、ありませんあなたが探しているデータを含んでいませんか? – Bergi