jquery-deferred

jQuery Deferred not calling the resolve/done callbacks in order

心不动则不痛 提交于 2019-11-26 22:02:32
问题 Code example: http://jsfiddle.net/MhEPw/1/ I have two jQuery Deferred objects. I want to have more than one 'async' request happening - and after they all run I want the callbacks (the .done functions) to be run in order they were specified in. Unfortunately they don't run in order. Maybe I am looking for some functionality here that Deferred doesn't provide? 回答1: What you need to do is link all of your request with one master deferred object and register all of your callbacks on its promise.

How to chain ajax calls using jquery

笑着哭i 提交于 2019-11-26 21:31:52
I need to make a series of N ajax requests without locking the browser, and want to use the jquery deferred object to accomplish this. Here is a simplified example with three requests, but my program may need to queue up over 100 (note that this is not the exact use case, the actual code does need to ensure the success of step (N-1) before executing the next step): $(document).ready(function(){ var deferred = $.Deferred(); var countries = ["US", "CA", "MX"]; $.each(countries, function(index, country){ deferred.pipe(getData(country)); }); }); function getData(country){ var data = { "country":

jQuery $.when() with variable arguments

微笑、不失礼 提交于 2019-11-26 20:52:49
问题 I want to send [1, n) AJAX-requests to the server and after all have returned a result a modal dialog should be closed. $.when(a(), b(), c()) would be perfect, but I don't know how to pass the variable count of functions to $.when as parameter. Any ideas how to solve this problem? 回答1: Call the functions and add their return values to an array. Then call $.when passing the array as argument like so: $.when.apply($, array) See Function.prototype.apply [MDN] for more information and my answer

Throwing an Error in jQuery's Deferred object

廉价感情. 提交于 2019-11-26 18:59:41
I have an $.ajax promise and want to check whether my (syntactically valid) response contains an error, triggering the rejected status in that case. I have worked with my own promise library which deals such tasks easily. I do not really like jQuery's Promise ( cache ) implementation with its Deferred object and may have overlooked something, because I seldom use it. I think the way to go is just using .then() , which seems to be rather complicated: return $.ajax(...).then(function success(response) { var problem = hasError(response); if (problem) { var error = new $.Deferred; error.reject

jQuery: execute array of functions sequentially (both deferreds and non-deferreds)

安稳与你 提交于 2019-11-26 18:25:01
问题 I am fairly new to using Promises and have a hard time of wrapping my head around jQuery deferreds. What I currently have is an array of functions which I execute at a certain point: while (queue.length) { (queue.shift())(); } The problem with this is, that some of those functions are asynchronous, but I need them to run one after another. So some of the functions in the queue return a deferred (e.g. via jQuery.ajax()) and some are just normal functions. I would like to know if how it would

When should I use jQuery deferred's “then” method and when should I use the “pipe” method?

匆匆过客 提交于 2019-11-26 18:11:47
jQuery's Deferred has two functions which can be used to implement asynchronous chaining of functions: then() deferred.then( doneCallbacks, failCallbacks ) Returns: Deferred doneCallbacks A function, or array of functions, called when the Deferred is resolved. failCallbacks A function, or array of functions, called when the Deferred is rejected. pipe() deferred.pipe( [doneFilter] [, failFilter] ) Returns: Promise doneFilter An optional function that is called when the Deferred is resolved. failFilter An optional function that is called when the Deferred is rejected. I know then() has been

What are deferred objects?

戏子无情 提交于 2019-11-26 18:05:23
jQuery 1.5 adds "Deferred Objects". What are they, and what exactly do they do? Deferred Object As of jQuery 1.5, the Deferred object provides a way to register multiple callbacks into self-managed callback queues, invoke callback queues as appropriate, and relay the success or failure state of any synchronous or asynchronous function. Deferred Methods: deferred.done() Add handlers to be called when the Deferred object is resolved. deferred.fail() Add handlers to be called when the Deferred object is rejected. deferred.isRejected() Determine whether a Deferred object has been rejected.

How to make all AJAX calls sequential?

為{幸葍}努か 提交于 2019-11-26 17:43:27
I use jQuery. And I don't want parallel AJAX calls on my application, each call must wait the previous before starting. How to implement it? There is any helper? UPDATE If there is any synchronous version of the XMLHttpRequest or jQuery.post I would like to know. But sequential != synchronous, and I would like an asynchronous and sequential solution. There's a much better way to do this than using synchronous ajax calls. Jquery ajax returns a deferred so you can just use pipe chaining to make sure that each ajax call finishes before the next runs. Here's a working example with a more in depth

jQuery Deferred's, $.when() and the fail() callback arguments

你说的曾经没有我的故事 提交于 2019-11-26 14:19:50
问题 I'm getting an unexpected result when using $.when() when one of the deferred operations does not succeed. Take this JavaScript, which created 2 deferreds. The first one succeeds and the second one fails. var f1 = function() { return $.Deferred(function(dfd) { dfd.resolve('123 from f1'); }).promise(); }; var f2 = function() { return $.Deferred(function(dfd) { dfd.reject('456 from f2'); }).promise(); }; $.when(f1(), f2()) .then(function(f1Val, f2Val) { alert('success! f1, f2: ' + JSON

Reactjs async rendering of components

≯℡__Kan透↙ 提交于 2019-11-26 12:21:19
问题 I want to render my component after my ajax request is done. Below you can see my code var CategoriesSetup = React.createClass({ render: function(){ var rows = []; $.get(\'http://foobar.io/api/v1/listings/categories/\').done(function (data) { $.each(data, function(index, element){ rows.push(<OptionRow obj={element} />); }); return (<Input type=\'select\'>{rows}</Input>) }) } }); But i get the error below because i am returning render inside the done method of my ajax request. Uncaught Error: