Javascript
1module.exports.getHoge = (userId, date) => new Promise((resolve, reject) => { 2 ...省略 3 4 Promise.all([hoge1(userId, date), hoge2(userId, date)]) 5 ... 省略 6 7 .then(() => { 8 console.log('3 then'); 9 10 connection.connect(); 11 connection.query(`SELECT * FROM hoge_photos WHERE app_id = ${appId};`, (err, rows, fields) => { 12 if(err){ 13 console.log('mysql2 err'); 14 console.log(err); 15 throw err; 16 } else { 17 console.log('ここが期待通りじゃない'); 18 console.log(rows); 19 _.forEach(rows, (v, k) => { 20 arrPhoto.push(`${s3BucketURL}/${userId}/${v.app_id}/${rows[k].photo_hoge}`); 21 isPhotoData[arrKey]['photo'] = arrPhoto; 22 }); 23 } 24 }); 25 26 }) 27 .then(() => { 28 console.log('4 then'); 29 resolve(_.union(isPhotoData, isNoPhotoData)); 30 }); 31});
上記のようなコードがあるのですが
これを実行すると
1 then 2 then 3 then 4 then ここが期待通りじゃない [ TextRow { hoge: hoge } ]
と3 then
の後にここが期待通りじゃない
処理が来てほしいのですが
connection.query
の部分が非同期処理のために4 then
の後に結果がきているのが原因だと思っています。
このような場合、私が期待している3 then
の後に
connection.query
の結果がくるにはどうしたら良いのでしょうか?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。