jquery-deferred

jQuery deferred - do I need pipes or chains to achieve this pattern?

回眸只為那壹抹淺笑 提交于 2019-11-27 02:25:10
问题 I'm trying to implement the folowing scenario, using JQuery deferred, without much luck. What parts of the deferred api would you use, and how would you structure your calls to achieve the following: 1st ajax callA to serviceA retrieve a list of Ids wait until this call returns then n ajax calls to serviceB, each call using a using an Id from the list returned by callA wait until all serviceB calls have returned then a final ajax call to serviceC 回答1: You could do like this (more or less

Using jQuery load with promises

主宰稳场 提交于 2019-11-27 02:09:52
问题 I'm still trying to wrap my head around deferred and what not, so with this in mind I have a question on how to do the following. My team and I have 3 separate .load() methods that each go grab a specific template and append that to the same container. Each load takes a different amount of time as you might think, so when the content loads, it loads in a 'stair step' fashion (1, then 2, then 3). I'd like to make use of deferred objects and wait until they are all done, then append them at the

Asynchronous Loop of jQuery Deferreds (promises)

拥有回忆 提交于 2019-11-27 00:56:00
问题 I am trying to create what I think is referred to as a "Waterfall". I want to sequentially process an array of async functions (jQuery promises). Here's a contrived example: function doTask(taskNum){ var dfd = $.Deferred(), time = Math.floor(Math.random()*3000); setTimeout(function(){ console.log(taskNum); dfd.resolve(); },time) return dfd.promise(); } var tasks = [1,2,3]; for (var i = 0; i < tasks.length; i++){ doTask(tasks[i]); } console.log("all done"); I would like it to complete the task

javascript function wait until another function to finish

偶尔善良 提交于 2019-11-27 00:24:10
问题 I have two javascript functions that are called from android. After long debug sessions finally I realized that the problem is arising from the fact that second function is getting called before first one is finished. I already searched the examples with deferred etc, but they all depends on function calls within another one. function FunctInit(someVarible){ //someVariable is sent from android, cannot call again from getResult //init and fill screen } function getResult(){ //also getResult

Retry a jquery ajax request which has callbacks attached to its deferred

和自甴很熟 提交于 2019-11-27 00:07:04
问题 I'm trying to implement a system of retrying ajax requests that fail for a temporary reason. In my case, it is about retrying requests that failed with a 401 status code because the session has expired, after calling a refresh webservice that revives the session. The problem is that the "done" callbacks are not called on a successful retry, unlike the "success" ajax option callback that is called. I've made up a simple example below: $.ajaxSetup({statusCode: { 404: function() { this.url = '

Reactjs async rendering of components

女生的网名这么多〃 提交于 2019-11-26 23:47:05
I want to render my component after my ajax request is done. Below you can see my code var CategoriesSetup = React.createClass({ render: function(){ var rows = []; $.get('http://foobar.io/api/v1/listings/categories/').done(function (data) { $.each(data, function(index, element){ rows.push(<OptionRow obj={element} />); }); return (<Input type='select'>{rows}</Input>) }) } }); But i get the error below because i am returning render inside the done method of my ajax request. Uncaught Error: Invariant Violation: CategoriesSetup.render(): A valid ReactComponent must be returned. You may have

Check Internet connectivity with jquery

让人想犯罪 __ 提交于 2019-11-26 23:10:18
问题 I am trying to check for the internet connection by sending a GET request to the server. I am a beginner in jquery and javascript. I am not using navigator.onLine for my code as it works differently in different browsers. This is my code so far: var check_connectivity={ is_internet_connected : function(){ var dfd = new $.Deferred(); $.get("/app/check_connectivity/") .done(function(resp){ return dfd.resolve(); }) .fail(function(resp){ return dfd.reject(resp); }) return dfd.promise(); }, } I

Looping through jQuery Deferreds after all promises have been called

戏子无情 提交于 2019-11-26 23:07:54
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 have been added to my array. Now to my problem. Since the then() method is only called once , when I

Asynchronous JavaScript - Callbacks vs Deferred/Promise [duplicate]

混江龙づ霸主 提交于 2019-11-26 22:39: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",

Deferred versus promise

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 22:29:47
问题 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 = $