dojo_Dojo DeferredList

dojo

Deferreds are all the rage in the JavaScript community these days and who can argue?  Deferreds, an object representing an asynchronous action, make working with AJAX requests incredibly easy -- no callback hell, no problem passing request information around.  What if you want to work with multiple Deferreds though?   For example, say you want to execute functionality once multiple AJAX requests complete.  The Dojo Toolkit has you covered with dojo/DeferredList, a resource whose API mirrors a single Deferred object but is capable of handling multiple Deferreds.

如今,递延在JavaScript社区中风靡一时,谁能争辩? Deferreds是一个代表异步操作的对象,它使处理AJAX请求变得异常简单-无需回调,也无需传递请求信息。 但是,如果您想使用多个Deferred,该怎么办? 例如,假设您要在多个AJAX请求完成后执行功能。 Dojo Toolkit为您提供了dojo / DeferredList ,该资源的API镜像单个Deferred对象,但能够处理多个Deferred。

使用延期 (Using Deferreds)

Dojo returns Deferred objects from many operations, including animation and XHR requests.  With the Deferred object, you can use the then property to react to the response once the async interaction is completed:

Dojo通过许多操作(包括动画和XHR请求)返回Deferred对象。 使用Deferred对象,可以在异步交互完成后使用then属性对响应做出React:


// A very basic usage of dojo/request
request("service.php?id=someid").then(function(response) {
	console.log("request result is:", response);
});


The example above shows a basic AJAX request and handling of the resulting Deferred.  Easy, right?  But what if you want an action to execute only after multiple Deferreds have resolved?

上面的示例显示了基本的AJAX请求和对结果Deferred的处理。 容易吧? 但是,如果您希望仅在解决多个延迟后才执行操作,该怎么办?

使用dojo / DeferredList (Using dojo/DeferredList)

The DeferredList is a manager for multiple Deferreds, making handling multiple Deferreds a breeze:

DeferredList是多个Deferred的管理器,使处理多个Deferred变得轻而易举:


// Require the Dojo dependencies
require(["dojo/request", "dojo/DeferredList"], function(request, DeferredList) {
	console.log("Request loaded!", request);

	// Request 1
	var promise1 = request("/endpoint/1").then(function(response) {
		console.log("request 1 result", response);
	});

	// Request 2
	var promise2 = request("/endpoint/2").then(function(response) {
		console.log("request 2 result", response);
	});

	// Create a DeferredList to manage both of them
	var list = new DeferredList([promise1, promise2]);
	// When they're both resolved...
	list.then(function(result) {
		// result is:  [Array[2], Array[2]]
		// result item[0] is the result of each request

		// Do something!
		
	});
});


The example above represents all roses:  all successful Deferreds.  What if a Deferred fails;  a 404 error, request timeout, or a Deferred rejection?  A little validation cures all that:

上面的示例代表所有玫瑰:所有成功的Deferreds。 如果推迟失败怎么办? 404错误,请求超时还是延迟拒绝? 稍作验证即可解决所有问题:


// Require the Dojo dependencies
require(["dojo/request", "dojo/DeferredList"], function(request, DeferredList) {
	console.log("Request loaded!", request);

	// Request 1
	var promise1 = request("/endpoint/1").then(function(response) {
		console.log("request 1 result", response);
	});

	// Request 2
	var promise2 = request("/endpoint/2").then(function(response) {
		console.log("request 2 result", response);
	});

	// Request 3:  A request that will fail...
	var promise3 = request("/endpoint/noexist").then(function(response) {
		console.log("request 3 result (fail)", response);
	});

	// Create a DeferredList to manage both of them
	var list = new DeferredList([promise1, promise2, promise3]);
	// When they're both resolved...
	list.then(function(result) {
		if(request[0][0] && request[1][0] && request[2][0]) { // request[2][0] is false
			// Success!
		}
		else {
			// React to a failure
		}
	});
});


If the promise for a given Deferreds returns false, you know the request (or async action) failed.  The second argument returned by the Deferred provides information about the request and why it failed:

如果给定Deferred的承诺返回false,则您知道请求(或异步操作)失败。 Deferred返回的第二个参数提供有关请求及其失败原因的信息:


{
	"message": "Unable to load noexist.php status: 404",
	"response": {
		"url": "/endpoint/noexist",
		"options": {
			"headers":{}
		},
		"xhr": {
			"statusText": "Not Found",
			"status": 404,
			"response": "{404 page html}"}
		}
}


The example here is oversimplified.  One realistic example is the one I used to create the dojox/mobile TweetView example, sending multiple JSONP requests (Twitter profile and Twitter timeline list) for a client-side Twitter widget.  The dojo/DeferredList resource makes handling multiple asynchronous actions easy and enjoyable.  Just another reasons to use the Dojo Toolkit in your next web application!

这里的示例过于简单。 一个现实的例子是我用来创建dojox / mobile TweetView例子的例子 ,它为客户端Twitter小部件发送多个JSONP请求(Twitter个人资料和Twitter时间轴列表)。 dojo / DeferredList资源使处理多个异步操作变得轻松而愉快。 在下一个Web应用程序中使用Dojo Toolkit的另一个原因!

翻译自: https://davidwalsh.name/deferredlist

dojo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值