node.js - Node js handle await in nested loops -
how can handle multiple async/await inside nested loops?
this code:
export default async (req) => { const report = req.body.webaudits; const def = deferred(); if(report.length == 0) return []; var reportlist = []; for(const item of report.entries()) { for(const [runindex, run] of item.runs.entries()) { const result = await wacompliancebusiness(req, run.id); var failurelist = []; if (!result.data.overviews) { continue; } const compliance = result.data.overviews[0].compliance; if(!compliance) { continue; } for(const rule of compliance.entries()) { const response = await waruleoverview(req, run.id, rule.id); const { failedconditions, rulename } = response.data; if(response.data.pagesfailed === 0) continue; for(let condition of failedconditions) { const request = { itemid: condition.conditionresult.id, itemtype: condition.conditionresult.idtype, parentid: condition.conditionresult.parentid, parenttype: condition.conditionresult.parenttype } const body = { runid: run.id, ruleid: rule.id, payload: request } const failure = await waconditionoverview(req, body); const description = failure.data.description; const conditionvalues = failure.data.conditionvalues; var valuelist = []; conditionvalues.foreach(item => { const value = (item.value == "") ? 'empty' : item.value; if(valuelist.indexof(value) == -1) { valuelist.push(value); } }) var actualvalue = ''; valuelist.foreach((value, v => { actualvalue += value; if(v > 0) { actualvalue += '\n'; } }); var expected = description.expected[0] ? description.name + ' ' + description.matcher + ' ' + description.expected[0] : description.name + ' ' + description.matcher if(description.idtype == "variable") { var failureobj = { rulename: rulename, expected: expected, actual: description.name + '=' + actualvalue, }; } else if(description.idtype == "tag") { var failureobj = { rulename: rulename, expected: description.name + '\n' + description.matcher, actual: actualvalue, }; } failurelist.push(failureobj); } } if(failurelist.length > 0) { item.runs[runindex].failures = failurelist; } } } return report; } express route:
const router = express.router(); router.post('/', authenticate, async (req, res) => { const runresponse = { webaudits: [], webjourneys: [], appjourneys: [] }; console.log('router'); const webaudits = await webauditsfailures(req); runresponse.webaudits = webaudits; res.status(201).json({ reports: runresponse }); console.log('success'); }) my code runs sequentially, problem function webauditsfailures re-called if execution didn't finish. printed console:
here check wa here check wa success
i can't understand happening , i'm doing wrong ... there 2 weeks since i'm facing issue , try not working...
if there other ways implement without using async/await, can use them.
here wacompliancebusiness,waruleoverview , waconditionoverview functions:
import deferred 'deferred'; export function wacompliancebusiness(req, runid, callback) { var def = deferred(); const apitoken = req.currentuser.apitoken; const payload = { 'authorization': 'api_key ' + apitoken } const options = { 'method': 'get', 'gzip': true, 'headers': payload, 'content-type': 'application/json', 'json': true, 'url': 'api-url' } request(options, (error, response, body) => { def.resolve(body); }); return def.promise; } export function waruleoverview(req, runid, ruleid) { var def = deferred(); const apitoken = req.currentuser.apitoken; const payload = { 'authorization': 'api_key ' + apitoken } const options = { 'method': 'get', 'gzip': true, 'headers': payload, 'content-type': 'application/json', 'json': true, 'url': 'api-url' } request(options, (error, response, body) => { def.resolve(body); }); return def.promise; } export function waconditionoverview(req, body) { var def = deferred(); const apitoken = req.currentuser.apitoken; const payload = { 'authorization': 'api_key ' + apitoken } const options = { 'method': 'post', 'gzip': true, 'headers': payload, 'body': body.payload, 'content-type': 'application/json', 'json': true, 'url': 'api-url' } request(options, (error, response, body) => { def.resolve(body); }); return def.promise; }
Comments
Post a Comment