promise解决异步问题

例子:
解决保存和查询的异步
1.给保存方法加promise

 preserve() {
      // 修改方法
      // 点新增之后选择了,再点修改 这时datalist 里面的数据全是str
      // 要获取查询列表里面这一行里面的数据
      //机房名称是这一行里面的motorRoom
      var that = this;
      that.loading = true;
      let joinId = that.paperFlag;
      return new Promise((resolve, reject) => {
      。。。
        resolve("success")和reject(error)
        }

2.给查询方法加promise(标准的promise的用法)

  faultPointDataListByPAGE(pageNo, pageSize) {
      let that = this;
      that.loading = true;
      let joinId = that.paperFlag;
      return new Promise((resolve, reject) => {
        that.dataList = [];
        try {
          db.all(
            "SELECT  id, userId, motorRoom, equipName, firstConfig, secondConfig, joinId, point, create_by, create_date, dyfield1, dyfield2, dyfield3, dyfield4, dyfield5, equipCname, firstConfigName, secondConfigName, motorRoomName from xf_exam_dataconfig_tp where joinId = '" +
              joinId +
              "' order by id",
            function(err, res) {
              console.log(res);
              if (res != undefined && res != null && res.length > 0) {
                that.dataList = res;
                that.rebuildData();
              } else {
                that.dataList = [];
              }
              resolve("success");
            }
          );
        } catch (error) {
          console.log(error);
          reject(error);
        }
        that.loading = false;
      });
    },

3.解决异步问题

preserveTrue() {//保存按钮的方法
      let that = this;
      that
        .preserve()
        .then(res => {
          if (res == "success") {
            return that.faultPointDataListByPAGE(0, 1000);
          }
        })
        .then(res => {
          if (res == "success") {
            that.$message({
              type: "success",
              message: "保存成功!"
            });
          }
        });
    },

你可能感兴趣的:(promise)