I have a problem with async/await and some Promises.
I have this code. It starts here:
let valid = await LoadRouter.load(body); console.log(valid);//showing me Pending Promises The function is:
loadGeneratingUnits(data){ let newUGArray = []; try { const result = data.map(async (itemGU, index) => { const checGU = await this.checkDataGu(itemGU.nombre); if(!checGU){ let newUG = { generating_unit_name: itemGU.nombre, description: (!itemGU.descripcion) ? null : itemGU.descripcion, it_generating_unit_id: (!itemGU.it_unidad_generadora) ? 0 : itemGU.it_unidad_generadora } newUGArray.push(newUG); } }) return result; } catch (error) { throw new Error(error.message) } } This one is where I have the problems
async checkDataGu(guName = null){ if(guName){ return await generatingUnitModel.findOne({ attributes: [ 'id', 'generating_unit_name', ], where: { generating_unit_name: guName } }) } }
Any comment about the use of async/await on this code?
result.mapis now returning an array of promises since the mapper function returns promises (it's async!)newUGArray. Should it?