jquery-deferred

Can I pass Promises to jQuery.when(), or only Deferreds?

旧城冷巷雨未停 提交于 2019-12-04 03:46:25
问题 The documentation for jQuery.when() says that this function takes Deferreds. However, it also says later on that: If a single argument is passed to jQuery.when() and it is not a Deferred or a Promise... which seems to imply that it can take Promises as well. But Promises are not Deferreds - they have a subset of the Deferred's methods. I guess you could say that a Deferred is a Promise, but a Promise is not a Deferred. Questions: Can $.when() take either Promises or Deferreds? This seems to

How to fetch value from Promise object after promise has been resolved

余生颓废 提交于 2019-12-04 02:42:49
Please note This is a contrived example. function longFunc(){ var deferred = $.Deferred(); setTimeout(function(){ console.log("long func completed"); deferred.resolve("hello"); }, 3000); return deferred.promise(); } function shortAfterLongFunc(x){ console.log('short func completed with value: ' + x); return { a: x }; } processFurther(longFunc().then(shortAfterLongFunc)); // send the array for further processing Problem I am unable to figure out how to return any kind of object/function for further downstream processing after shortAfterLongFunc completes. I can console.log from

not asynchronous function executed as jQuery Deferred

为君一笑 提交于 2019-12-04 01:13:09
Lets say I want to process some tasks in the synchronous manner, so I have this function: function executePromiseQueueSync(queue){ var seed = $.Deferred(), finalPromise; finalPromise = _.reduce(queue, function(memo, promise){ return memo.then(function(){ return promise.funct.apply(null, promise.argmnt); }); }, seed.promise()); seed.resolve(); return finalPromise; } Now I can use it to process some files: _.each(fileList, function(element, index, list){ _.each(element, function(el, idx, lst){ promisesQueue.push({funct: processFile, argmnt:[el, index + (len - fileList.length) ,len]}); }); });

When should I reject a promise?

寵の児 提交于 2019-12-03 23:51:15
I'm writing some JS code that uses promises. For example, I open a form pop-up and I return a jQuery Deferred object. It works like this: If the user clicks OK on the form, and it validates, the Deferred resolves to an object representing the form data. If the user clicks Cancel, then the Deferred resolves to a null. What I'm trying to decide is should the Deferred instead reject, instead of resolve? More generally, I'm wondering when should I resolve to something like a null object, and when should I reject? Here's some code demonstrating the two positions: // Resolve with null. var promise =

jquery: what happens when you pass a deferred object into “then”?

£可爱£侵袭症+ 提交于 2019-12-03 21:09:50
I have been researching jquery deferred objects for a while now and I'm stumped on one thing. The "done," "always," "then," etc methods take, as their arguments, functions which should be called when the deferred object is resolved. However I tried chaining requests by passing a deferred object into the "always" method, and that seems to work too: // A is an array of data var req = ajax(url + "?d=" + A[0]).done(processResults).fail(reportFailure); for (var i = 1 ; i < A.length ; i++) { var tmp = ajax(url + "?d=" + A[i]).done(processResults).fail(reportFailure); req.always(tmp); req = tmp; }

how to create a jQuery loading bar? (like the ones used on flash sites)

感情迁移 提交于 2019-12-03 18:09:54
问题 I have multiple elements I need to load before the user has control over the page (mostly images, videos, and audio). Currently, I'm loading them with the $.when() function, like this: //all elements are hidden, except for a black background and //a "loading" gif in the middle. $.when($.ajax("video1.ogv"), $.ajax("video2.ogv"), $.ajax("videoN.ogv")) .then(function () { //show the site with all the content preloaded... }); Is there any way to create a loading bar that shows the progress (in

in JavaScript, how to wrap a promise in timeout?

随声附和 提交于 2019-12-03 10:13:52
It's a common pattern to implement timeout of some asynchronous function, using deffered/promise: // Create a Deferred and return its Promise function timeout(funct, args, time) { var dfd = new jQuery.Deferred(); // execute asynchronous code funct.apply(null, args); // When the asynchronous code is completed, resolve the Deferred: dfd.resolve('success'); setTimeout(function() { dfd.reject('sorry'); }, time); return dfd.promise(); } Now we can execute some asynchronous function called myFunc and handle timeout: // Attach a done and fail handler for the asyncEvent $.when( timeout(myFunc, [some

Can jQuery deferreds be cancelled?

那年仲夏 提交于 2019-12-03 09:20:57
I have a situation where I want to cancel a deferred. The deferred is associated with an ajax call. Why I am using deferreds I don't use the normal xhr objects returned by $.ajax. I'm using jsonp, which means I can't use HTTP status codes for error handling and have to embed them in the responses. The codes are then examined and an associated deferred object is marked as resolved or rejected accordingly. I have a custom api function that does this for me. function api(options) { var url = settings('api') + options.url; var deferred = $.Deferred(function(){ this.done(options.success); this.fail

Chaining multiple ajax calls with jquery deferred objects

走远了吗. 提交于 2019-12-03 08:26:34
While looking for a solution similar to as described here: How to chain ajax calls using jquery I am looking for a solution using jquery v1.52. I have a set of ajax requests to be made. But each ajax request is to be sent only after completing the previous ajax call. I am trying to achieve this with jquery 1.5.2 but just unable to. Here is what I modified from the above mentioned example. It does not work. Can anyone help me get this working? Expected output is http://jsfiddle.net/k8aUj/3/ P.S: I cannot upgrade to version beyond 1.5.2 Sandy Yo! Solved! http://jsfiddle.net/sandhyasriraj/AaHZv/

Deferred and Ajax

为君一笑 提交于 2019-12-03 07:55:50
Reading a JSON-Service like this: $.ajax({ url:'activeIDs', success : function(data){ // data = [14,15] var tableRows = []; for (var dataIndex=0; dataIndex < data.length; dataIndex++) { var isLast = dataIndex == (data.length - 1); $.ajax({ url: 'info?id=' + data[dataIndex], success: function(data2) { // "foo", "bar" tableRows.push(data2.name); if (isLast) { alert(tableRows.length); } } }); } } }); First network-trace is: activeIDs = [14,15] info?id=14 (takes 2 seconds) = "foo" info?id=15 (takes 4 seconds) = "bar" In this case the alert gives "2". Seconds network-trace is different: activeIDs =