deferred

Multiple ajax calls from array and handle callback when completed

北慕城南 提交于 2019-11-26 19:45:36
问题 I have used promises in jQuery slightly before - but I am having trouble applying it to this scenario. I prefer to use the $.when() and $.done() methods to achieve this. From what I understand I need to build a $.Deferred object which logs the requests and when those requests are finished - fire the callback. In my code below the callback is firing before the ajax requests and not after - maybe I just need some sleep I know my code is incomplete I have been struggling to apply it with the

Do we need to close the response object if an error occurs while calling http.Get(url)?

£可爱£侵袭症+ 提交于 2019-11-26 16:44:20
问题 In the following code is it also necessary to close the response body in the error case: res, err := http.Get(url) if err != nil { log.Printf("Error: %s\n", err) } defer res.Body.Close() 回答1: General concept is that when a function (or method) has multi return values one being an error , error should be checked first and only proceed if the error is nil . Functions should return zero values for other (non-error) values if there is an error . If the function behaves differently, it should be

Promises for promises that are yet to be created without using the deferred [anti]pattern

喜欢而已 提交于 2019-11-26 16:43:55
Problem 1: only one API request is allowed at a given time, so the real network requests are queued while there's one that has not been completed yet. An app can call the API level anytime and expecting a promise in return. When the API call is queued, the promise for the network request would be created at some point in the future - what to return to the app? That's how it can be solved with a deferred "proxy" promise: var queue = []; function callAPI (params) { if (API_available) { API_available = false; return doRealNetRequest(params).then(function(data){ API_available = true;

Simple approach to launching background task in Django

折月煮酒 提交于 2019-11-26 16:14:40
问题 I have a Django website, and one page has a button (or link) that when clicked will launch a somewhat long running task. Obviously I want to launch this task as a background task and immediately return a result to the user. I want to implement this using a simple approach that will not require me to install and learn a whole new messaging architecture like Celery for example. I do not want to use Celery! I just want to use a simple approach that I can set up and get running over the next half

Using $.Deferred() with nested ajax calls in a loop

孤人 提交于 2019-11-26 15:47:55
问题 I've spent far too many hours searching for similar questions and trying solutions, so I hope someone has a solution. Basically, I would like to be notified when a function a() has completed. The problem is that the function contains an ajax call and a loop that calls b(), which again contains an ajax call. UPDATED WITH FIDDLE: http://jsfiddle.net/hsyj7/1/ Like so: // called by main() function a() { return $.ajax("http://url1").pipe(function(data){ for (var i = 0; i < 2; i++) { console.log('a

How to chain execution of array of functions when every function returns deferred.promise?

点点圈 提交于 2019-11-26 13:48:31
问题 I have created my first deferred object in Node.js using deferred module and it works great when I pass result to next function and trigger resolve and reject.How to chain execution of array of functions when every function returns deferred.promise ? I have like input parameters array of functions and input parameter for first function and every next function get parameter from previous. It works like f1(100).then(f2).then(f3) , but how when I have n number of functions. 回答1: You need to

Looping through jQuery Deferreds after all promises have been called

余生长醉 提交于 2019-11-26 12:19:46
问题 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

AngularJS : Where to use promises?

三世轮回 提交于 2019-11-26 11:57:49
I saw some examples of Facebook Login services that were using promises to access FB Graph API. Example #1 : this.api = function(item) { var deferred = $q.defer(); if (item) { facebook.FB.api('/' + item, function (result) { $rootScope.$apply(function () { if (angular.isUndefined(result.error)) { deferred.resolve(result); } else { deferred.reject(result.error); } }); }); } return deferred.promise; } And services that used "$scope.$digest() // Manual scope evaluation" when got the response Example #2 : angular.module('HomePageModule', []).factory('facebookConnect', function() { return new

How to block for a javascript promise and return the resolved result? [duplicate]

情到浓时终转凉″ 提交于 2019-11-26 09:13:27
问题 This question already has an answer here: How do I return the response from an asynchronous call? 36 answers I am obviously misunderstanding something about either the way js promises are resolved or about the semantics of \"return.\" I am being called by a function that expects me to be synchronous - to return a value. Calculating that value requires some asynchronous code (specifically, the ForEach method on a dstore Collection What I\'m trying to accomplish is approximately this, but this

How to defer routes definition in Angular.js?

◇◆丶佛笑我妖孽 提交于 2019-11-26 08:06:13
问题 I have configured some basic routes that are available for all users before they log in: App.config(function ($routeProvider) { $routeProvider. when(\'/login\', { templateUrl: \'views/login.html\', controller: PageStartCtrl.Controller }). otherwise({ redirectTo: \'/login\' }); }); So the only thing user can do is to log in. After the user logs in, I would like to register additional routes like this: $http .post(\'api/Users/Login\', { User: userName, Password: userPassword }) .success