js方法保持同步promiseAll

单个请求保持同步-方法执行完再执行后面的语句

delItem(item) {
      if (item.row.status == '2'){
        this.$message.warning("需求在开发中,无法删除");
        return;
      }
      if (item.row.status == '3'){
        this.$message.warning("需求已开发完成,无法删除");
        return;
      }
  //  后端请求的数据接口执行完,再执行弹窗this.$confirm
      list({demandUnid: item.row.unid}).then((res)=>{
        let bindTask = []
        bindTask = res.data.data
        if(bindTask.length > 0 && bindTask !== null){
          this.$message.warning("无法删除,请先删除需求关联的开发任务");
          return
        }
        this.$confirm("是否删除此项", "提示", {
          confirmButtonText: "是",
          cancelButtonText: "否",
          type: "warning",
        })
            .then(() => {
              return remove(item.row.unid);
            })
            .then(() => {
              this.$util.showMessage("删除成功", "1");
              this._reset();
            })
            .catch((err) => {
              if (err != "cancel") {
                this.$message.error(err);
              }
            });
      })
    },
  • list({demandUnid: item.row.unid}).then((res)=> 将需要顺序执行的语句放到后面,这样可以保持同步

for循环多个请求保持同步-方法执行完,才执行后面的语句

  • 先执行所有每个后端接口才执行删除操作promise.all方法
delItems() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      let pormiseData = []
      for (let i = 0; i < this.selectionList.length; i++) {
        let item = this.selectionList[i];
        if (item.status == '2'){
          this.$message.warning("需求在开发中,无法删除");
          return;
        }
        if (item.status == '3'){
          this.$message.warning("需求已开发完成,无法删除");
          return;
        }
        let x = list({demandUnid: item.unid}).then((res)=>{
          let bindTask = []
          bindTask = res.data.data
          if(bindTask.length > 0 && bindTask !== null){
            this.$message.warning("无法删除,请先删除需求关联的开发任务");
            return 1
          }
        })
        pormiseData.push(x)
      }
      // 同步执行(重点)
      Promise.all(pormiseData).then((res)=>{
        let delFlag = res.filter((item)=> item == 1)
        if (delFlag.length == 0 || delFlag == null){
          this.$confirm("确定将选择数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
              .then(() => {
                return remove(this.unids);
              })
              .then(() => {
                this.$util.showMessage("删除成功", "1");
                this._reset();
              })
              .catch((err) => {
                if (err != "cancel") {
                  this.$message.error(err);
                }
              });
         }
      })
    },
  • 等待查询的后端数据全部返回,方法才返回数据promise.all方法
 handleDataBase(val) {
      if(this.databaseUnid !==""){
        getTables(val).then((res) => {
          let data = res.data.data;
          if (data.length > 0) {
            this.tableList = data;
            this.tableList.forEach((tableItem)=>{
              let tableNameKey = tableItem.name
              let tableComment = tableItem.comment
              this.tableCommentArr.push({tableName: tableNameKey, tableComment: tableComment})
            })
          }
          this.tableList.forEach((item, index) => {
            let primaryData = this.tableList[index].name;
            this.enterField(primaryData, index);
          })
        })
      }
    },
    submitData(){
      this.handleTable()
    },
    enterField(item) {
      // 需要同步的后端接口
      let x = getColumns(this.databaseUnid, item).then((res) => {
        let data = res.data.data;
        if (data.length > 0) {
            this.tableCommentArr.forEach((itemTableCommen)=>{
            if (itemTableCommen.tableName == item){
              let tableField = {tableName: item,tableComment: itemTableCommen.tableComment,tableField: data }
              this.tableFieldArr.push(tableField)
            }
          })
        }
        return 1
      });
      this.pormiseData.push(x)
    },
    handleTable() {
      // 等待需要接收的后端接口的数据全部接收完,才执行后面的语句
      Promise.all(this.pormiseData).then((res)=>{
        let flag = res.filter((item) => item != 1);
        if (flag.length == 0){
          let obj = {fieldsData: this.tableFieldArr,tableNames: this.tableList,databaseUnid: this.databaseUnid}
          this.$emit('customEvent', obj)
          this.closedDrawDBTableSelect()
        }
      })
    },
posted @ 2023-08-03 17:52  ElloeStudy  阅读(304)  评论(0编辑  收藏  举报