jquery-deferred

Chaining ajax requests with jQuery's deferred

十年热恋 提交于 2019-12-21 07:34:04
问题 I have a web app which must call the server multiple times. So far, I had a long nested callback chain; but I would like to use jQuery's when , then etc. functionality. However, I can't seem to get stuff running again after using a then . $ .when ($.get('pages/run-tool.html')) .then (function (args) { // This works fine alert(args); $('#content').replaceWith (args); $('#progress-bar').progressbar ({value: 0}); }) .then ($.get('pages/test.html')) .done (function(args) { // This prints the same

When should I reject a promise?

≡放荡痞女 提交于 2019-12-21 06:55:19
问题 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

When should I reject a promise?

谁说我不能喝 提交于 2019-12-21 06:53:23
问题 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

Implement Deferred object without using jquery

两盒软妹~` 提交于 2019-12-20 10:47:54
问题 I want to implement basic Deferred object without using jQuery. Here i will be implementing only done and fail callbacks, with resolve and reject functions. and ofCourse associating promise method with this function. i am doing the following implementation in pure js (Edited) : function Deferred() { var d = {}; d.resolve = function() { d.done(arguments); } d.reject = function() { d.fail(arguments); } d.promise = function() { var x = {}; x.done = function(args) { return args; } x.fail =

SAPUI5 Wait for an Deferred-Object // wait for .done() function

[亡魂溺海] 提交于 2019-12-20 07:20:32
问题 I know there are several threads on this, but I think in SAPUI5 context no thread answers this general topic on deferred/sync calls in SAPUI5. In My Controller I got : test : function() { var dfd = $.Deferred(); var sServiceUrl = '/sap/opu/odata/sap/xyz/MySet?$format=json'; var post = $.ajax({ url: sServiceUrl, type: "GET" }); post.done(function(data){ console.log(data); dfd.resolve(); }); post.fail(function(){ console.log("Error loading: " + sServiceUrl); dfd.reject(); }); return dfd.promise

Jquery promise wait to ajax end

牧云@^-^@ 提交于 2019-12-19 10:47:17
问题 I'm getting predefined values, which i have to insert into two selects: <div id="wrapper"> <select id="first"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <select id="second"></select> </div> Options inside #second depends on selected value in #first . This options are loaded via ajax: $('#first').change( function() { $.ajax({ url: "giveMeValues.phpOrWhatever" }).done(function() { // just simulating data from ajax call $('#second').append(

use jquery deferreds for variable number of ajax requests

你离开我真会死。 提交于 2019-12-19 10:41:19
问题 when I have a variable number of ajax requests, how can I call them using deferreds? my guess: //qty_of_gets = 3; function getHTML(productID, qty_of_gets){ var dfd = $.Deferred(), i = 0, c = 0; //this is where there could be some magic to //do multiple ajax posts //obviously I'm out of my depth here... while (i <= qty_of_gets){ dfd.pipe(function(){ $.get("queries/html/" + product_id + i + ".php"); }); i++ } dfd.done(function(){ while (c <= qty_of_gets){ $('myDiv').append(c); c++; } }); } 回答1:

Wait for the end of an asynchronous Javascript function to retrieve a result (Deferred ?)

浪尽此生 提交于 2019-12-19 04:14:46
问题 Ok guys, I know this topic has already been discussed multiple times, but I can't find an answer to solve my problem. So I'm trying to do a simple think : I'm creating a string, like : distance is : " + CalculateDistance(position); The wanted result is something like distance is 5kms (8min) . CalculateDistance(position) is a function calling a Google maps API called DistanceMatrix to calculate distance between two points. The API is documented here, and the given sample perfectly works. I

jQuery: What is the difference between deferred.always() and deferred.then()?

跟風遠走 提交于 2019-12-18 11:48:16
问题 Seems to me that both does the same thing. Docs: deferred.always() deferred.then() 回答1: It would seem that deferred.then() allows you to pass two separate callbacks for success and failure, whereas deferred.always() takes n number of callbacks which will all be called regardless of the outcome of the initial event. I would say use deferred.always() in the cases where success/failure of the initial event are not important 回答2: With .then() you can provide an individual callback for when the $

Iterate over indefinite array of deferred items

做~自己de王妃 提交于 2019-12-18 07:22:48
问题 Say I have a very long list where each element requires an asynchronous call to fetch. I would like to write an API on top of that list so that a consumer can simply call "next()" or otherwise synchronously iterate over the list. Ideally I would have something that looks like this: while ((foo = generator.next()) != null) { process(foo); } But, I find myself tripping over the semantics of deferred calls, and I don't know how to escape this hard-coded pattern into a generic loop: $.when(foo)