deferred

Chain ajax and execute it in sequence. Jquery Deferred

蓝咒 提交于 2019-11-27 07:19:49
I have 3 processes that needs ajax to complete. But it is asynchronous and it fails to do what I wanted to do.. Lets say: function a(param1, param2) { $.post(..., function(result){ if(result){ b(); } else { console.log("failed a"); } }) } function b() { $.post(..., function(result){ if(result){ c(); } else { console.log("failed b"); } }) } function c() { $.post(..., function(result){ if(result){ console.log("successful"); } else { console.log("failed b"); } }) } I want it to execute like this a b c That code will work perfectly, as you can see.. but if use a loop. var data = [{param1 : 1235,

zg手册 之 twisted 开发(2)-- Deferreds 组件

荒凉一梦 提交于 2019-11-27 04:25:12
Deferreds 异步回调序列 Deferred 本质上是一个回调函数的集合,twisted 提供了对函数延迟调用的机制。 在 twisted 内部,使用 Deferred 对象管理回调序列。当异步请求结果返回时,使用 Deferred 对象调用回调序列中的函数。 这里通过几个例子来了解 Deferreds 使用的方式和工作的原理。 先看一个简单的例子 from twisted.internet import reactor, defer def getDummyData(x): """创建一个 Deferred 对象,并返回这个对象""" d = defer.Deferred() # 2 秒钟后执行 Deferred 回调函数序列,把 x * 3 作为参数传递给回调序列中的第一个函数 # reactor.callLater 是一个定时延迟调用的方法 reactor.callLater(2, d.callback, x * 3) return d def printData(result): """ 打印结果 """ print resultd = getDummyData(3) # 添加回调函数到回调函数序列中 d.addCallback(printData) # 4 秒钟后停止 reactor 循环(退出进程) reactor.callLater(4, reactor

AngularJS promises not firing when returned from a service [duplicate]

故事扮演 提交于 2019-11-27 01:42:02
问题 Possible Duplicate: angularjs - promise never resolved in controller I'm wrapping a slow WebSockets server in a AngularJS service, and then calling into that service from my controllers. If I chain callbacks onto callbacks onto callbacks, everything works just fine, any the UI updates asynchronously. When I try to use $q.defer() to clean up that mess of callbacks, it seems my deferred never gets called. I'm familiar with the concepts of deferred from Python's Twisted, so I think conceptually

Defer usage clarification

有些话、适合烂在心里 提交于 2019-11-26 23:40:57
问题 Let's assume I have the following function func printNumbers(){ var x int defer fmt.Println(x) for i := 0; i < 5; i++{ x++ } } As it is said in the specification: Each time a "defer" statement executes, the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked. Obviously, zero will be printed out when the function execution ends. But what should I do if I want to print out the final value of variable x ? I've come up with the

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

How to defer routes definition in Angular.js?

冷暖自知 提交于 2019-11-26 21:56:21
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(function (response : any) { App.config(function ($routeProvider) { $routeProvider .when('/dashboard', { templateUrl:

How to chain ajax calls using jquery

笑着哭i 提交于 2019-11-26 21:31:52
I need to make a series of N ajax requests without locking the browser, and want to use the jquery deferred object to accomplish this. Here is a simplified example with three requests, but my program may need to queue up over 100 (note that this is not the exact use case, the actual code does need to ensure the success of step (N-1) before executing the next step): $(document).ready(function(){ var deferred = $.Deferred(); var countries = ["US", "CA", "MX"]; $.each(countries, function(index, country){ deferred.pipe(getData(country)); }); }); function getData(country){ var data = { "country":

AngularJS : $q -> deferred API order of things (lifecycle) AND who invokes digest?

半世苍凉 提交于 2019-11-26 20:31:52
The $q service is very powerful in angularjs and make our life easier with asynchronous code. I am new to angular but using deferred API is not very new to me. I must say that I completely ok with the How to use part of documentation + there are very useful links for that within the docs + I checked out the source either. My question is more about the under the hood parts of deferred and promise API objects in angular. What are the exact phases in their life cycles and how are they interacts with rootScope.Scope (s). My assumptions are that when the promise resolves - it invokes the digest

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

浪子不回头ぞ 提交于 2019-11-26 20:27:19
问题 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,

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

不想你离开。 提交于 2019-11-26 19:52:35
问题 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