JS forEach发送请求实现同步

Promise.all(
          this.idList.map( ({id}) =>{
            return new Promise(async (resolve, reject) =>{
              await this.getA(id).then(() => {
                this.getB(id).then(() => {
                  this.getC(id).then(()=>{
                    resolve();
                  })
                });
              });
            })
          })
        ).then(() =>{
          //forEach循环请求结束后执行的方法
        })

参考 js forEach循环调用异步方法,如何实现同步

for循环请求同步

  methods: {
  	......
	    getList(ids) {
	      return new Promise((resolve, reject) => {
	      	//listByIds api.js中声明的接口
	        listByIds(ids).then(response => {
	          resolve(response);
	        })
	      })
	    },
	    getIds(){
	      return new Promise((resolve, reject) => {
	    	Ids().then(response =>{
	    		resolve(response);
	    	})
	      })
	    }
	
		async getDatas(){
			this.ids = await this.getIds();
			this.dataList = await this.getList(this.ids);
		}
	.....
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐