deferred

AngularJS $q. Deferred queue

僤鯓⒐⒋嵵緔 提交于 2019-11-28 03:37:35
问题 I have array (f.e. it is queue of files): [{deferred: fileDef, data: file}, {...}, ...] Each fileDef and file send to upload function which return fileDef.promise and call fileDef.resolve or fileDef.reject after uploading. I want upload files in order: next file upload after previous file is loaded. Now I use var queue = []; var uploading = false; //file input callback call each time the user selects files function addAndUpload (file) { queue.push({deferred: $q.defer(), data: file}); if (

Is the deferred/promise concept in JavaScript a new one or is it a traditional part of functional programming?

纵然是瞬间 提交于 2019-11-27 20:45:29
I really like the idea of jQuery's deferred/promise pattern or paradigm but sometimes I have trouble wrapping my aging brain around the finer points or specific implementation details. In fact recently I've found that the deferred/promise pattern/paradigm seems to predate jQuery and is also in at least these other JavaScript libraries/frameworks: Deferred github Q homepage task.js homepage when.js github wire.js github , presentation YUI gallery-deferred module I've probably missed some, included stuff that's really part of one of the others, and made other mistakes in that list... Please edit

Simple approach to launching background task in Django

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 20:29:29
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 hour or so. Isn't there a simple way to do this in Django without having to add (yet another) 3rd

Js Deferred/Promise/Future compared to functional languages like Scala

折月煮酒 提交于 2019-11-27 19:25:08
I'm mostly using programming languages like Scala and JavaScript. I'm trying to understand the similarities and differences in how async reactive programming is used in both languages. Can you help me? I'm not taking any particular Js Promise framework because it seems many implement the similar specifications (like Promise/A). I've only used Q so far. It seems that in Javascript we call a Deferred the object we resolve to complete a Promise . In Scala, it seems the Promise is the object you resolve to get a Future monad. Can someone tell me if this is right? Is there any good reason for a

Multiple ajax calls from array and handle callback when completed

一世执手 提交于 2019-11-27 19:07:56
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 addition of the for loop. http://jsfiddle.net/whiteb0x/MBZEu/ var list = ['obj1', 'obj2', 'obj3', 'obj4',

What's the difference between a Deferred object and its own promise object?

不打扰是莪最后的温柔 提交于 2019-11-27 18:46:39
Let's create a simple Deferred object: defer = $.Deferred( function ( defer ) { setTimeout( defer.resolve, 3000 ); }); The above Deferred object will be in the "pending" state for 3 seconds, and then switch to the "resolved" state (at which point all the callbacks bound to it will be invoked). Let's also retrieve the promise of that Deferred object: promise = defer.promise(); Now, to add callbacks which are going to be invoked once the Deferred object is resolved, we can use .done() or .then() . However, we can invoke this method both on the Deferred object itself or its own promise object.

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

跟風遠走 提交于 2019-11-27 14:24:45
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() 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 documented. http.Get() does not document such deviation. So it should be handled like this: res, err := http

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

烈酒焚心 提交于 2019-11-27 11:55:33
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 called'); b(); } }); } // called by a() function b() { for (var i = 0; i < 2; i++) { $.ajax("http:/

How to always run some code when a promise is fulfilled in Angular.js

帅比萌擦擦* 提交于 2019-11-27 11:50:26
问题 In my Angular.js application, I'm running some asynchronous operation. Before it starts I cover the application with a modal div, then once the operation is complete, I need to remove the div, whether the operation was successful or not. Currently I have this: LoadingOverlay.start(); Auth.initialize().then(function() { LoadingOverlay.stop(); }, function() { LoadingOverlay.stop(); // Code needs to be duplicated here }) It works well, however I would prefer to have something cleaner like this

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

杀马特。学长 韩版系。学妹 提交于 2019-11-27 07:57:52
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. You need to build a promise chain in a loop: var promise = funcs[0](input); for (var i = 1; i < funcs.length; i++) promise =