.when

await for jQuery.when ajax requests

妖精的绣舞 提交于 2021-02-19 09:41:23
问题 This code inside async function, does not give the expected result: var result = await $.when( $.get('/api/1'), $.get('/api/2') ); with one request, result will be the output I expect (the response text). However, with these two requests, the returned result is an array which does not hold the two Promises values. Is there any workaround? I know there are then() and done() , but I prefer using await. 回答1: jQuery's .when() and the native await have different semantics. Compare: // jQuery $

$.when.done callback isn't working

别说谁变了你拦得住时间么 提交于 2020-01-13 06:37:07
问题 The following code has a bad syntax error. Probably because I'm using 'for' or something. $.when( for (var i=0; i < 5; i++) { $.getScript( "'" + someArr[i].fileName + ".js'"); } $.Deferred(function( deferred ) { $( deferred.resolve ); }) ).done(function() { alert("done"); }); I'm trying to call couple of scripts and then when trey are all loaded I want to an alert to show. 回答1: A commented (but untested) solution with the changes is below // When takes a promise (or list of promises), not a

JQuery deferred.done executes predefined function instantly, but not function declared inside

和自甴很熟 提交于 2020-01-05 08:00:33
问题 My problem is a weird one, as described in the title. Here's the code: Case 1: var first = $.ajax({ // about 500ms request url: myUrl success: function() { console.log(1); } }); var second = $.ajax({ // about 200 ms request url: myUrl success: function() { console.log(2); } }); $.when(first, second).done(function() { console.log(3); }); Logs 2, 1, 3. All good, just what I wanted. Case 2: var first = $.ajax({ // about 500ms request url: myUrl success: function() { console.log(1); } }); var

Tried $.when and $.then no lucky

删除回忆录丶 提交于 2019-12-25 02:25:33
问题 I am trying to display some words and then when it is complete display the full sentence. the problem is doesn't matter which I try, it plays all together. Not one after the other. Can one of you explain to me what I am doing wrong? function DisplayHdline( callback){ var str = "HELP STOP CRUELTY NOW."; var spans = '<span>' + str.split(/\s+/).join(' </span><span>') + '</span>'; jQuery(spans).hide().appendTo('#vidheadline').each(function(i) { jQuery(this).delay(2000 * i).fadeIn('fast').delay

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

Can I rely on the order of responses from jQuery.when on multiple Ajax requests

a 夏天 提交于 2019-12-13 18:28:38
问题 Per this post: https://stackoverflow.com/a/17548609/985704 Using jQuery.when to perform multiple simultaneous ajax requests. var requests = Array(); requests.push($.get('responsePage.php?data=foo')); requests.push($.get('responsePage.php?data=bar')); var defer = $.when.apply($, requests); defer.done(function(){ // This is executed only after every ajax request has been completed $.each(arguments, function(index, responseData){ // "responseData" will contain an array of response information

Wait until all jquery ajax request are done? (part 2)

隐身守侯 提交于 2019-12-12 06:45:13
问题 I checked this post and the answer is really good: Wait until all jQuery Ajax requests are done? But I just want to be more generic: I wonder how we can use this logic for a List of services (ajaxservices) and list of callbacks e.g. ajaxservices = ["url-getdata1", "url-getdata2"]; callbacks = [callbackdata1, callbackdata2]; callbackdata1 = function (data){...} $.when(/*somehow call all ajaxservices[]*/).done(function (dataList) { for (var i = 0; i < callbacks.length; i++) { callbacks[i]

How to make an array of Deferred objects

北城余情 提交于 2019-12-12 03:29:46
问题 Am new to Deferreds and Promises. Here is my [simplified] code, which is defined within a JavaScript object: myFunction: function(d, cb) { return $.ajax('/myURL', { contentType: 'application/json', data: d, dataType: 'json', type: 'POST' }).then(cb, cb); }, flush: function(myArray) { return myFunction(myArray, myCallback); } The above works fine. I can call flush(someArray), and some time later I get the result of the ajax request. QUESTION: I want to modify the flush function so that it

Executing one function after another

时光毁灭记忆、已成空白 提交于 2019-12-11 14:58:21
问题 All of these functions are in the main function which is called after jQuery async AJAX success event and it runs every second. Each of the three functions call specific functions within them as well. This is the basic javascript code I have: function mainFunction() { function selectView(){ // do stuff } function startWidgets(){ // do stuff } function popData(){ // do stuff } $.when(selectView()).then(startWidgets).then(popData); } function ajaxCall(){ ajaxRequest = $.ajax({ type: "GET", url:

How to use jQuery.when() with an array of URLs?

自闭症网瘾萝莉.ら 提交于 2019-12-11 11:56:39
问题 How would i have to change this example $.when( $.getScript( "/mypath/myscript1.js" ), $.getScript( "/mypath/myscript2.js" ), $.getScript( "/mypath/myscript3.js" ), $.Deferred(function( deferred ){ $( deferred.resolve ); }) ).done(function() { //place your code here, the scripts are all loaded }); when i don't know the exact number of scripts to load and use an array of URLs instead? var urls = [ '/url/to/script1.js', '/url/to/script2.js', '/url/to/script3.js', '/url/to/script4.js' ]; As the