How to solve the asynchronous problem of http requests in loops

    let paramArr = JSON.parse(paramArray)
    let data = [];
    paramArr.forEach(async (val, i) => {
      let easywlb = dasign ? tqyybm : (tqyybm.substr(0, 2) === "02" ? ("12" + tqyybm.substr(2, 4)) : ("11" + tqyybm.substr(2, 4)));
      let param = { "" };
      await this.post("", param);
      let result = this.get("data");
      
      for (let item in result) {
        data.push({ "a": result[item].a, "b": result[item].b });
      }
    })
    //data[]
    ctx.body = { data, success: this.success() }
    

how to solve the problems caused by multiple cyclic asynchronous operations in this process
Please explain in detail Thank you


forEach is not suitable for receiving such callback functions. The
async function returns Promise , so you can use map to get an array of Promise. You can promises= paramArr.map (async (val, i) = >. .
then use await Promise.all (promises) .

< hr >

I looked at your code again, data.push ({'asides: result [item] .a, 'baked: result [item] .b}); The order of here is uncertain, so there will be a problem with the order of data. You can either use the for loop to abandon concurrent requests, or you can finally sort data to get the same order as the original (index I is stored in the index I every time you push).

< hr >

of course, if data.push does not process immediately, but lets Promise return the result, then Promise.all is ordered and then stored in data.


const { EventEmitter } = require('events');

var event = new EventEmitter();

let paramArr = JSON.parse(paramArray);
let data = [];
paramArr.forEach(async (val, i) => {
    let easywlb = dasign
        ? tqyybm
        : tqyybm.substr(0, 2) === '02'
            ? '12' + tqyybm.substr(2, 4)
            : '11' + tqyybm.substr(2, 4);
    let param = {
        /* '' */
    };
    await this.post('', param);
    let result = this.get('data');
    for (let item in result) {
        data.push({ a: result[item].a, b: result[item].b });
    }
    event.emit('loaded');
});
//data[]
var count = 0;
event.on('loaded', function() {
    if (PPcount === paramArr.length) {
        event.emit('finish');
    }
});

event.on('finish', function() {
    ctx.body = { data, success: this.success() };
})

event monitoring style


const paramArr = JSON.parse(paramArray).map(async(val)=>{
    let easywlb = dasign ? tqyybm : (tqyybm.substr(0, 2) === '02' ? ('12' + tqyybm.substr(2, 4)) : ('11' + tqyybm.substr(2, 4)));
    let param = { '' };
    await this.post('', param);
    let result = this.get('data');
    
    const map = [];
    for (let item in result) {
      map.push({ 'a': result[item].a, 'b': result[item].b });
    }
    return map;
});

const data = await Promise.all(paramArr);

Don't use forEach, to directly for the loop, and it's not necessary. Asnyc/await, since the this.post method is promise, just call promise directly

let paramArr = JSON.parse(paramArray)
let data = [];
let promiseArr = paramArr.map(val=>{
    let easywlb = dasign ? tqyybm : (tqyybm.substr(0, 2) === '02' ? ('12' + tqyybm.substr(2, 4)) : ('11' + tqyybm.substr(2, 4)));
    let param = { '' };
    return this.post('', param).then(()=>{
        let result = this.get('data');
        return result.map(n=>return {a:n.a, b:n.b}))
    })
});
promise.all(promiseArr)
    .then((resultArr)=>ctx.body = { data: data.reduce((p,c)=>[p.concat(c), p][1],[]), success: this.success() })
    .catch(err=>console.log(err))
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bca222-5206c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bca222-5206c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?