Javascript
1module.exports.getHoge = (userId, date) => new Promise((resolve, reject) => { 2 let hoges = []; 3 4 Promise.all([getHogeIncluded(userId, date), getHogeExcluded(userId, date)]) 5 .then((arrHoge) => { 6 if (arrHoge[0][0]) { 7 8 =============> console.log('1'); 9 10 const foreachfunction = new Promise((resolve, reject) => { 11 _.forEach(arrHoge[0], (val, key) => { 12 13 =============> console.log('2'); 14 15 knex 16 .select('hoge_name') 17 .from('Hoge_hoge') 18 .where('hoge_id', val.hoge_id) 19 .then((hogeNames) => { 20 21 =============> console.log('3'); 22 23 _.forEach(hogeNames, (v, k) => { 24 hoges.push(`${URL}/${hogeId}/${val.hoge_id}/${hogeNames[k].hoge_name}`); 25 }); 26 27 =============> console.log('4'); 28 29 arrHoge[0][key].hoge = hoges; // eslint-disable-line no-param-reassign 30 hoges = []; 31 }); 32 }); 33 resolve(arrHoge); 34 }); 35 36 foreachfunction 37 .then(() => { 38 39 =============> console.log('5'); 40 41 }) 42 43 } 44 }) 45});
このような関数を実行すると
1 2 2 2 2 5 3 4 3 4 3 4 3 4
となります。
全てのforEach
の処理後の結果が欲しいのですが途中で5
が呼ばれしまっている状況にあります。
初心者ながらPromise
を使っているのですが私が実現したいことはどのように書き換えれば可能なのでしょうか...。
ご教授していただけると幸いです。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。