Jquery Deferred 链式

JQuery Deferred Chain

JQuery Deferred 链式参数参数写要then

$.get("http://localhost/Designer/Handlers/CommonService.ashx")

.then(function(data){

debugger;

return $.get("http://localhost/Designer/Handlers/StepActorConfigActualActor.ashx")

}).then(function(data){

debugger;})

 

 

如果你使用done 第二个done 中函数的参数还是第一个done的参数

 $.get("http://localhost/Designer/Handlers/CommonService.ashx")
	.done(function(data){
	debugger; 
	return $.get("http://localhost/Designer/Handlers/StepActorConfigActualActor.ashx")
	}).done(function(data){
	debugger;})

 

 

ES5 Promse 链式写法的参数传递

fetch("http://localhost/Designer/Handlers/CommonService.ashx",{credentials: "include"})

.then(function(response){

debugger;

return fetch("http://localhost/Designer/Handlers/StepActorConfigActualActor.ashx",{credentials: "include"})

}).then(function(response){debugger;});

 

 

参数 https://stackoverflow.com/questions/17216438/chain-multiple-then-in-jquery-when

转载于:https://my.oschina.net/fsilence/blog/1583877

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jQuery 中的 Deferred 对象提供了一种优雅的方式来处理异步操作,它可以让我们更方便地控制异步操作的状态和执行顺序。下面是 Deferred 对象的常用方法: 1. $.Deferred():创建一个 Deferred 对象。 2. deferred.done():当 Deferred 对象的状态变为已完成时,调用该方法注册的回调函数。 3. deferred.fail():当 Deferred 对象的状态变为已失败时,调用该方法注册的回调函数。 4. deferred.always():无论 Deferred 对象的状态是已完成还是已失败,都调用该方法注册的回调函数。 5. deferred.then():当 Deferred 对象的状态变化时,调用该方法注册的回调函数。它可以接受两个参数,第一个参数是已完成状态的回调函数,第二个参数是已失败状态的回调函数。 6. deferred.promise():返回一个 Promise 对象,该对象可以被传递给其他函数或者方法,但只能调用 then()、catch() 和 finally() 方法,不能改变 Deferred 对象的状态。 下面是一个使用 Deferred 对象的示例代码: ```javascript function asyncAction() { var defer = $.Deferred(); setTimeout(function() { defer.resolve("Async Action Completed!"); }, 2000); return defer.promise(); } var promise = asyncAction(); promise.then(function(data) { console.log(data); }).fail(function() { console.log("Async Action Failed!"); }).always(function() { console.log("Async Action Done!"); }); ``` 在这个例子中,我们定义了一个 asyncAction() 函数,它返回一个 Deferred 对象。在该函数内部,我们使用 setTimeout() 函数模拟一个异步操作,并在两秒后调用 resolve() 方法,将 Deferred 对象的状态设置为已完成。然后我们使用 promise 变量保存该 Deferred 对象的 Promise 对象,并使用 then()、fail() 和 always() 方法注册回调函数。在 then() 方法中,输出异步操作完成的信息;在 fail() 方法中,输出异步操作失败的信息;在 always() 方法中,输出异步操作完成的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值