使用$.when实现异步调用的链式写法

解决回调地狱的问题

this.actionId = queryParam('actionId');
    $.when(this.isActive())
    .done(() => { //成功执行的函数体
      window.getToken = this.getToken;
      window.getImei = this.getImei;
      this.initEvent();
      this.initActInfo();
    })
    .fail( ()=> {
      androidJs.doAndroidAction("toastError",JSON.stringify({toastString: '当前活动已关闭'})) //失败执行的函数体
    });

isActive(){
      let dtd = $.Deferred();
      $.ajax('/action/check?actionId='+this.actionId+'').then( (res) => {
        if(res.value)
          dtd.resolve(); #返回Deferred状态
        else
          dtd.reject();
      })
      return dtd;
    }

你可能感兴趣的:(使用$.when实现异步调用的链式写法)