JS 怎么让整体的for同步执行而for中的异步函数异步执行

JavaScript010

JS 怎么让整体的for同步执行而for中的异步函数异步执行,第1张

看了你的问题,才百度学习的Promise,不知道是不是你要的效果。

   function async1(){

    return new Promise(function(resolve, reject) {

     setTimeout(function() {//setTimeout模拟异步

      console.log('async1 is done')

      resolve('async1 value')

     }, 6000)

    })

   }

   function async2(){

    return new Promise(function(resolve, reject) {

     setTimeout(function() {//setTimeout模拟异步

      console.log('async2 is done')

      resolve('async2 value')

     }, 3000)

    })

   }

   function async3(){//async3不需要then,所以没写return new Promise

    setTimeout(function() {

     console.log('async3 is done')

    }, 1000)

   }

   let p

   for(let i=0i<4i++){

    p=async1().then(async2)

   }

   p.then(async3)

axios.post('/operationlog/list', moduleParams).then((res) =>{if (res.status === 200) { this.moduleList = res.data.items

stepParams.append('category', "")

stepParams.append('needPlayback', true) for (let i = 0i <this.moduleList.lengthi++) {

stepParams.set('category', this.moduleList[i].category)

axios.post('/operationlogitem/list', stepParams).then((res) =>{ console.log(res.data.items)

}).catch((e) =>{ console.log(e)

})

}

}

}

代码如上,根据外层请求获取到多个不同的category,但是for循环是同步,里面的post请求是异步的,所以始终以最后一项的category为参数发送请求.请问如何修改代码按照每一项的category参数发送post请求