• 七校项目中使用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理解为请求成功时执行的

  • 相关阅读:
    【9018:2221】[伪模板]可持久化线段树
    【9018:2208】可持久化线段树2
    【9018:2207】可持久化线段树1
    【POJ2187】Beauty Contest
    2017/11/22模拟赛
    2017/11/3模拟赛
    [AtCoder 2702]Fountain Walk
    [AtCoder3856]Ice Rink Game
    20170910模拟赛
    20170906模拟赛
  • 原文地址:https://www.cnblogs.com/theworldofbeisong/p/9533120.html
Copyright © 2020-2023  润新知