GETルートを持つノード/エクスプレスサーバーがあります。このルートからクライアントに、インデックス付きオブジェクト配列で構成されたレスポンスを返す必要があります。各オブジェクトにはbase64エンコードイメージを取得する 'url'プロパティがありますbase64-img
というノード・ライブラリーを使用します。内部非同期コードを持つオブジェクトループのオブジェクトを反復するときに、変数を正しく追跡する方法はありますか?
オブジェクトのオブジェクトから始まり、各オブジェクトは、画像へのURLアドレスを持つ文字列であるプロパティ 'url'を持っています。 lodash
では、オブジェクトのオブジェクトをループしています。各ループの繰り返しは、現在の反復のobject.url
プロパティを受け取り、そのURLのターゲットイメージをbase64
エンコーディングに変換するbase64Img.requestBase64
を呼び出します。すべてのオブジェクトが繰り返し処理された後、オブジェクトのINDEXED配列(0,1,2など)の応答をクライアントに送信します。
ループが完了して非同期フローが原因ですべてのイメージが正しくエンコードされていることを確認する方法がわかりません。次のコードは機能しませんが、時々私は完全な配列を持つことがありますが、時にはそうではありませんが、クライアントは常にオブジェクトを含む配列を受け取って配列を返します。助けてください。
/*
userProfileImages = {
{url: 'http://www.someurl.com/blah1'},
{url: 'http://www.someurl.com/blah2'},
{url: 'http://www.someurl.com/blah3'},
{url: 'http://www.someurl.com/blah4'}
}
*/
let completeRequests = 0;
let arrayOfUserProfileImages = [];
_.map(userProfileImages, (userProfileImage, key) => {
completeRequests++;
let tmpCount = completeRequests;
let tmpUserProfileImage = userProfileImage;
// loop through each of the userProfileImages,
// get each image by the url in userProfileImage
base64Img.requestBase64(
tmpUserProfileImage.url,
function(error, messageRes, body) {
// if image conversion successful, proceed
if (!error) {
// get the base64 code and set it as the new
// value of the userProfileImage url property
tmpUserProfileImage.url = body;
// push the object into the array
arrayOfUserProfileImages.push(tmpUserProfileImage);
console.log('ARRAY LENGTH:', arrayOfUserProfileImages.length);
console.log(arrayOfUserProfileImages);
// if number of request matches the total number
// of requests to complete, we are done, return
// the appropriate json response
if (tmpCount === numberOfRequestsToMake) {
console.log(arrayOfUserProfileImages);
return res.json({ userProfileImages: arrayOfUserProfileImages });
}
// else return json error
} else {
return res.json({error: error});
}
} // end callback
); // end base64Img
}); // end _.map
ありがとう、これは私のすべての問題を解決しました。これらのasync/await/Promiseの概念を詳しく説明する、お勧めのリソースやオンラインクラス(udemy)はありますか?私は本当にこれをすべて知っていて、これを握っているのは難しい時です。ありがとうございました。 –
申し訳ありません私は便利です。おそらく、非同期関数が最初にどのように働くのかを説明するものが必要なので、一般的な概念を得てからそれを約束に変換すれば、async/awaitは簡単なジャンプです。 – Matt
ここには[それぞれの良い概要](https://blog.risingstack.com/asynchronous-javascript/)がありますが、イントロ/チュートリアルではあまりありません。 – Matt