jquery-deferred

Looping through jQuery Deferreds after all promises have been called

余生长醉 提交于 2019-11-26 12:19:46
问题 I am currently trying to build a File Uploader using the HTML5 FileAPI. The File Uploader is supposed to handle multiple files and show image previews if the file is an image. since the FileReader class works asynchronously I want to wait until all the the files have been read. Therefore I am using Deferreds. A method which reads the file is returning a promise. Another method loops through all files and pushes all promises into an array. Then I\'m applying the then() method once all promises

how does jquery's promise method really work?

£可爱£侵袭症+ 提交于 2019-11-26 10:07:39
问题 I don\'t really understand what delegate and promise are. According to the docs - delegate would bind a selector and event to some sort of wrapping container that can be used again at a later time for current and future items. promise() would remap things back to when it was first bounded if everything newly loaded matches. Maybe I don\'t really understand this promise method. What if the wrapper is still there, but the contents in the wrapper container have changed, and/or reloaded via Ajax

$.Deferred: How to detect when every promise has been executed

六眼飞鱼酱① 提交于 2019-11-26 09:00:50
I have a number of async tasks that need to be completed, so I'm using promises. I need to detect when each one of the promises has been executed (both resolved and rejected). I must not continue execution until that point. I was using something like this: $.when(promise1, promise2, ...).always(); But this code is wrong, because the when method has lazy evaluation, and it returns as soon as one of the promises fails. So the always callback also runs as soon as one of the promises fail. I was thinking in coding a workaround, but this use case is so common that maybe somebody has done it already

what is difference between success and .done() method of $.ajax

戏子无情 提交于 2019-11-26 08:55:37
问题 Can anyone help me? I am not able to understand the difference between success and .done() of $.ajax . If possible please give examples. 回答1: In short, decoupling success callback function from the ajax function so later you can add your own handlers without modifying the original code (observer pattern). Please find more detailed information from here: https://stackoverflow.com/a/14754681/1049184 回答2: success only fires if the AJAX call is successful, i.e. ultimately returns a HTTP 200

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

谁说胖子不能爱 提交于 2019-11-26 08:54:23
问题 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.

How to chain ajax calls using jquery

丶灬走出姿态 提交于 2019-11-26 07:59:48
问题 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

Throwing an Error in jQuery's Deferred object

六月ゝ 毕业季﹏ 提交于 2019-11-26 06:43:58
问题 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

What are deferred objects?

匆匆过客 提交于 2019-11-26 06:13:02
问题 jQuery 1.5 adds \"Deferred Objects\". What are they, and what exactly do they do? 回答1: 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

How to make all AJAX calls sequential?

时光毁灭记忆、已成空白 提交于 2019-11-26 06:06:56
问题 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. 回答1: 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

jQuery.when - Callback for when ALL Deferreds are no longer 'unresolved' (either resolved or rejected)?

随声附和 提交于 2019-11-26 03:34:15
问题 When multiple Deferred objects are passed to jQuery.when, the method returns the Promise from a new \"master\" Deferred object that tracks the aggregate state of all the Deferreds it has been passed. The method will either resolve its master Deferred as soon as ALL the Deferreds resolve, or reject its master Deferred as soon as ONE of the Deferreds is rejected. If the master Deferred is resolved (ie. ALL the Deferreds resolve), it is passed the resolved values of all the Deferreds that were