promise函数中正常跳出

如图,需求在执行红框所示return处代码达到以下效果:
1. 打断整个checkin的执行并跳出checkin
2. 该promise函数为正常结束(resolve),后续执行then

(只需要给出写法不用改原函数 自己看着都长)

img


--****--

//代码区
//此函数定义在page内
checkin(serverDate){
    return new Promise((resolve,reject)=>{
      var hour=new Date(serverDate).getHours();
      var minute=new Date(serverDate).getMinutes();
      var w=new Date(serverDate).getDay();
      var phone = parseInt(app.globalData.phone);
      var y=`${year}`;
      if (month<10) {
        var m='0'+`${month}`
      } else {
        var m=`${month}`
      }
      if (today<10) {
        var d='0'+`${today}`
      } else {
        var d=`${today}`
      }
      if (hour<12) {
        db.collection("classtime1").where({_id:`${w}`+'A'}).get().then( res => {
          if (res.data.length>0) {
            var classid=res.data[0].class;
            var classn=res.data[0].class+'.0';
            var classm=res.data[0].class+'.1';
            var checkinhour=parseInt(res.data[0].startime.split(":")[0]);
            var checkinminute=parseInt(res.data[0].startime.split(":")[1]);
            //签到
            if (!(res.data[0].startime == null || res.data[0].startime === '')  &&  (hour*60+minute+16>checkinhour*60+checkinminute)) {
              checkin1.where({_id:phone}).get().then( res=> {
                if (res.data[0][y+m+d+'A'] != '#FFFFFF') {
                  checkinsuccess=true;
                  return;
                }
                if (locationavailabe){
                  if (hour*60+minute-2<checkinhour*60+checkinminute) {
                    wx.showToast({
                      title: '签到已成功',
                      icon: 'success',
                      duration: 3000,
                    })
                    checkin1.where({_id:phone}).update({
                      data: {
                      [[y+m+d+'A']] : '#44e036'  //准时
                    },
                    
                  })
                  } else if (hour*60+minute-17<checkinhour*60+checkinminute) {
                    wx.showToast({
                      title: '迟到啦以后早起',
                      image: '/assets/images/checkin/late.png',
                      duration: 3000,
                    })
                    checkin1.where({_id:phone}).update({
                      data: {
                      [[y+m+d+'A']] : '#FFF200'  //迟到
                      },
                    })
                    crecord.where({_id:phone}).update({
                      data: {
                        [[classn]] : db.command.inc(1)  //迟到
                      },
                    })
                    
                  } else {
                    wx.showToast({
                      title: '缺勤啦以后早睡',
                      image: '/assets/images/checkin/absence.png',
                      duration: 3000,
                    })
                    checkin1.where({_id:phone}).update({
                      data: {
                      [[y+m+d+'A']] : '#ED1C24'  //缺勤
                      },
                    })
                    crecord.where({_id:phone}).update({
                      data: {
                        [[classm]] : db.command.inc(1)  //缺勤
                      },
                    })
                    
                  }
                  this.getNowDay(this.data.year,this.data.month,1);
                  checkinsuccess=true;
                  checkinavailable=true;
                }
                resolve();
              })
            } else {
              checkinavailable=false;
              resolve();
            }
            //签到后反馈该课程签到状况
            class1.where({_id:parseInt(classid)}).get().then( res=> {
              this.setData({
                total2:Math.floor(0.2*res.data[0]['total']),
                total5:Math.floor(0.5*res.data[0]['total'])
              })
            })
            crecord.where({_id:phone}).get().then( res=> {
              var recordlate=res.data[0][classid][0];
              var recordabsence=res.data[0][classid][1];
              if (recordlate>13) {
                recordlate=13
              }
              if (recordabsence>Math.floor(0.5*res.data[0]['total'])) {
                recordabsence=Math.floor(0.5*res.data[0]['total'])
              }
              if (recordlate>0) {
                if (recordlate<4) {
                  this.setData({
                    record1: false,
                    record2: true,
                  })
                } else {
                  this.setData({
                    record1: false,
                    record3: true,
                  })
                }
              }
              if (recordabsence>0) {
                if (recordabsence<Math.floor(0.2*res.data[0]['total'])+1) {
                  this.setData({
                    record1: false,
                    record4: true,
                  })
                } else {
                  this.setData({
                    record1: false,
                    record5: true,
                  })
                }
                this.setData({
                  recordlate: recordlate,
                  recordabsence: recordabsence,
                })
              }
            })
          }
        })
      } else {
        db.collection("classtime1").where({_id:`${w}`+'P'}).get().then( res => {
          if (res.data.length>0) {
            var classid=res.data[0].class;
            var classn=res.data[0].class+'.0';
            var classm=res.data[0].class+'.1';
            var checkinhour=parseInt(res.data[0].startime.split(":")[0]);
            var checkinminute=parseInt(res.data[0].startime.split(":")[1]);
            if (!(res.data[0].startime == null || res.data[0].startime === '')  &&  (hour*60+minute+16>checkinhour*60+checkinminute)  &&  (hour*60+minute<checkinhour*60+checkinminute+180)) {
              checkin1.where({_id:phone}).get().then( res=> {
                if (res.data[0][y+m+d+'P'] != '#FFFFFF') {
                  checkinsuccess=true;
                  return;
                }
                if (locationavailabe){
                  if (hour*60+minute-2<checkinhour*60+checkinminute) {
                    wx.showToast({
                      title: '签到已成功',
                      icon: 'success',
                      duration: 3000,
                      
                    })
                    checkin1.where({_id:phone}).update({
                      data: {
                      [[y+m+d+'P']] : '#44e036'  //准时
                      },
                    })
                  } else if (hour*60+minute-17<checkinhour*60+checkinminute) {
                    wx.showToast({
                      title: '迟到啦下次注意',
                      image: '/assets/images/checkin/late.png',
                      duration: 3000,
                    })
                    checkin1.where({_id:phone}).update({
                      data: {
                      [[y+m+d+'P']] : '#FFF200'  //迟到
                    },
                    })
                    crecord.where({_id:phone}).update({
                      data: {
                        [[classn]] : db.command.inc(1)  //迟到
                      },
                    })
                    
                  } else {
                    wx.showToast({
                      title: '缺勤啦下次注意',
                      image: '/assets/images/checkin/absence.png',
                      duration: 3000,
                    })
                    checkin1.where({_id:phone}).update({
                      data: {
                      [[y+m+d+'P']] : '#ED1C24'  //缺勤
                      },
                    })
                    crecord.where({_id:phone}).update({
                      data: {
                        [[classm]] : db.command.inc(1)  //缺勤
                      },
                    })
                    
                  }
                  this.getNowDay(this.data.year,this.data.month,1);
                  checkinsuccess=true;
                  checkinavailable=true;
                }
                resolve();
              })
            } else {
              checkinavailable=false;
              resolve();
            }
            class1.where({_id:parseInt(classid)}).get().then( res=> { console.log(res.data[0]);
              this.setData({
                total2:Math.floor(0.2*res.data[0]['total']),
                total5:Math.floor(0.5*res.data[0]['total'])
              })
            })
            crecord.where({_id:phone}).get().then( res=> {
              var recordlate=res.data[0][classid][0];
              var recordabsence=res.data[0][classid][1];
              if (recordlate>13) {
                recordlate=13
              }
              if (recordabsence>Math.floor(0.5*res.data[0]['total'])) {
                recordabsence=Math.floor(0.5*res.data[0]['total'])
              }
              if (recordlate>0) {
                if (recordlate<4) {
                  this.setData({
                    record1: false,
                    record2: true,
                  })
                } else {
                  this.setData({
                    record1: false,
                    record3: true,
                  })
                }
              }
              if (recordabsence>0) {
                if (recordabsence<Math.floor(0.2*res.data[0]['total'])+1) {
                  this.setData({
                    record1: false,
                    record4: true,
                  })
                } else {
                  this.setData({
                    record1: false,
                    record5: true,
                  })
                }
                this.setData({
                  recordlate: recordlate,
                  recordabsence: recordabsence,
                })
              }
            })
          }
        })
      }
    })
  },

但是 js 的 Promise,只要调用了 resolve,函数就会停止执行啊,所以只要在红框那里写

resolve();

就行了。

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632