循环中调用接口, 待数据全部循环完成后执行 Promise.all

save(type){
    
    
    let list = [];
    if(type == 2){
    
    
        list = this.imgData1;
    }else{
    
    
        list = this.imgData
    }
    let arr = [];
    list.forEach((file,index) => {
    
    
        let item = file.fileList[0];
        if(!item.id){
    
    
            var form = new FormData();
            form.append("myfile", item.raw);
            arr.push(new Promise((resolve,reject) => {
    
    
                uploadFile(form)
                .then(response => {
    
    
                    let {
    
    filepath,name} = response.data;
                    let data = {
    
    
                        image_index:String(index),
                        image_path:filepath,
                        app_type:type
                    }
                    addSlideshowImage(data)
                    .then(res => {
    
    
                        resolve(res)
                    })
                })
            }))
        }
    })
    //循环全部结束后执行
    Promise.all(arr).then((result)=>{
    
    
        console.log(arr)
        this.$message.success('保存成功');
        this.getSlideshowImage()
    })
},

猜你喜欢

转载自blog.csdn.net/weixin_42407989/article/details/110948810