jquery-deferred

Asynchronous JavaScript - Callbacks vs Deferred/Promise [duplicate]

冷暖自知 提交于 2019-11-27 18:06:13
Possible Duplicate: What are the differences between Deferred, Promise and Future in Javascript? Lately I've been making an effort to improve the quality of my JavaScript applications. One pattern I've adopted is to use a separate "data context" object to load data for my application (previously I was doing this directly in my view models). The following example returns data that is initialized on the client: var mockData = (function($, undefined) { var fruit = [ "apple", "orange", "banana", "pear" ]; var getFruit = function() { return fruit; }; return { getFruit: getFruit } })(jQuery); In

Deferred versus promise

ⅰ亾dé卋堺 提交于 2019-11-27 16:50:20
What is the difference between Deferred and Promise other than the jQuery versions? What should I use for my need? I only want to call the fooExecute() . I only need the fooStart() and fooEnd() to toggle the html div status for example. //I'm using jQuery v2.0.0 function fooStart() { /* Start Notification */ } function fooEnd() { /* End Notification */ } function fooExecute() { /* Execute the scripts */ } $('#button1').on('click', function() { var deferred1 = $.Deferred(); var promise1 = $.Promise(); deferred1.??? promise1.??? }); Felix Kling First: You cannot use $.Promise(); because it does

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

半世苍凉 提交于 2019-11-27 14:33:06
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 be possible to execute them in order + sequentially (executing the next function only when the previous

jQuery jqXHR - cancel chained calls, trigger error chain

你说的曾经没有我的故事 提交于 2019-11-27 12:53:37
I am creating a ajax utility for interfacing with my server methods. I would like to leverage jQuery 1.5+ deferred methods from the object returned from the jQuery.ajax() call. The situation is following. The serverside method always returns a JSON object: { success: true|false, data: ... } The client-side utility initiates the ajax call like this var jqxhr = $.ajax({ ... }); And the problem area: jqxhr.success(function(data, textStatus, xhr) { if(!data || !data.success) { ???? // abort processing, trigger error } }); return jqxhr; // return to caller so he can attach his own handlers So the

Chain multiple “then” in jQuery.when

放肆的年华 提交于 2019-11-27 09:41:08
问题 I have a function that does something like this: function do_something() { // some code return $.when(foo, bar, baz).then(do_something_else); } function do_something_else(_foo, _bar, _baz) { // do something else return /* the original inputs */; } So, when someone uses do_something , they can also chain more callbacks, like: do_something().then(function(_foo_2, _bar_2, _baz_2) { console.log(_foo_2, _bar_2, _baz_2); }); The problem is that I don't know how to bypass the original return from do

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

空扰寡人 提交于 2019-11-27 08:51:38
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.stringify([f1Val, f2Val])); }) .fail(function(f1Val, f2Val) { alert('fail! f1, f2: ' + JSON.stringify([f1Val,

How can I determine if a jQuery object is deferred?

你说的曾经没有我的故事 提交于 2019-11-27 06:33:32
问题 If I have a function that sometimes returns a deferred object but sometimes a non-deferred object. How can I tell which one it is? 回答1: Depending on your use case, you could also use jQuery.when [1]: If a single argument is passed to jQuery.when and it is not a Deferred, it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately. With jQuery.when you can treat your mysterious object always as deferred: // x could be a deferred object or an immediate

jQuery.when understanding

懵懂的女人 提交于 2019-11-27 03:10:56
I am trying to use the jQuery.when to fire two ajax requests and then call some function after the two requests have completed. Here's my code: var count = 0; var dfr; var showData = function(data) { dfr.resolve(); alert(count); // Do something with my data data received }; var method1 = function() { dfr = $.Deferred(); return $.ajax('localhost/MyDataService/DataMethod_ReturnsData', { dataType: "jsonp", jsonp: "$callback", success: showData }); }; var method2 = function() { return $.ajax('localhost/MyDataService/DataMethod_ReturnsCount', { dataType: "jsonp", jsonp: "$callback", success:

Ignoring AJAX errors with promises array and $.when

心不动则不痛 提交于 2019-11-27 02:57:32
问题 I have the following code that gets the JSON from an array of YouTube video ids. It works great when all the videos exists and the query is successful. It sends several getJSON request, and when all of them are done... the $.when.done() fires and I can process the resulting data. var results = {}, promises = []; $(document).ready(function() { var vids = [ 'ozj2-bnTL3s', 'EAZ4Tlt8MQ4', 'Xn9o7cxqVoA' // ,'this-videoid-doesnot-exists' ], url = 'http://gdata.youtube.com/feeds/api/videos/{{vid}}?v

Run function after another one completes

感情迁移 提交于 2019-11-27 02:31:40
问题 function1 = function(){ something.on('transitionend', function(){ // now function2 should run }); } function2 = function(){ alert('ok'); } function1(); function2(); So I heard about jQuery promises. I would return a "deferred" object, and inside the event handler I would call deferred.resolve(); But what happens if i have multiple event handlers there and I only want the next function to run when all have been fired? + I don't like the idea of introducing something foreign like "deferred"