zoukankan      html  css  js  c++  java
  • 七校项目中使用promise处理回调地狱问题

    //封装一个promise
    var ajaxPromise = function(param){
    		return new Promise(function(suc,fail){
    			debugger;
    			console.log(param)
                           //ajax请求
    			$.ajax({
    				url:param.url,
    				type:"post",
    				async:false,
    				data:param.data,
    				dataType:"json",
    				success: function(res) {
    					suc(res);
    				      },
    				error: function(err) {
    				   fail(err);
    		      }
    			})
    		})
    	}
    	var step1 = ajaxPromise({url:'Student_getStudentInfo',data:{phoneNum:sessionPhoneNum}})
    		.then(function(res){
    			console.log("jjjjj");
                            //返回一个新的之前封装的promise
    			return ajaxPromise({url:"ApplyForm_getSchoolList",data:{}})
    		})
    		.then(function(res){
    			console.log("gsjgj");
    		},function(err){
    			console.log("jfdhskjfhsdfhsj");
    		})
    		.catch(function(err){
    			console.log(ajaxPromise);
    			console.log("fkdjskjsdk");
    		})
    });    
    

      then方法里面可以有两个函数  第一个是当将Promise的状态变为fuifill的时候,后面一个函数是将promise状态变为reject的时候,

         但是其实catch也可以处理这样的reject 以及本来就需要处理的异常的报错所以不能简单党的将then理解为请求成功时执行的

  • 相关阅读:
    处理excel表
    生成登陆验证码
    IKAnalyzer分词例子
    JAVA中不用+ 如何实现加法
    开发者应该掌握的Java代码优化技能
    Spring Boot Maven插件
    23种设计模式(1)-单例模式
    Spring AOP 创建切面
    Spring AOP 创建增强类
    Spring AOP基础知识
  • 原文地址:https://www.cnblogs.com/theworldofbeisong/p/9533120.html
Copyright © 2011-2022 走看看