closures

Any better way to combine multiple callbacks?

你。 提交于 2019-12-01 19:03:13
I need to call an async function (with loop) for multiple values and wait for those results. Right now I'm using the following code: (function(){ var when_done = function(r){ alert("Completed. Sum of lengths is: [" + r + "]"); }; // call when ready var datain = ['google','facebook','youtube','twitter']; // the data to be parsed var response = {pending:0, fordone:false, data:0}; // control object, "data" holds summed response lengths response.cb = function(){ // if there are pending requests, or the loop isn't ready yet do nothing if(response.pending||!response.fordone) return; // otherwise

“Mutable variable is accessible from closure” in a function passed to Array.prototype.every

ぃ、小莉子 提交于 2019-12-01 18:58:12
Code will speak more plainly than I will: var candidateIndex = 0; var minValue = Number.MAX_VALUE; topArray.every(function(element, index) { if (element.innerArray && element.innerArray.length < minValue) { minValue = element.innerArray.length; candidateIndex = index; if (minValue == 0) { return false; } } return true; }); // ... use minValue and candidateIndex What this is doing is going through the topArray , and finding either the first member of that array that has an innerArray of length 0, otherwise finding the one that has the smallest length innerArray . It's working fine, but the

How do I access variables that are inside closures in Swift?

只谈情不闲聊 提交于 2019-12-01 18:44:18
I'm new to Swift and I'm trying to get the result from this function. I don't know how to access variables that are inside the closure that is passed to the sendAsynchronousRequest function from outside the closure. I have read the chapter on closures in the Apple Swift guide and I didn't find an answer and I didn't find one on StackOverflow that helped. I can't assign the value of the 'json' variable to the 'dict' variable and have that stick outside of the closure. var dict: NSDictionary! NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler:

Closures behaving differently in for and foreach loops

我与影子孤独终老i 提交于 2019-12-01 18:27:32
问题 While experimenting with closures in C# I found out that they work rather unexpectedly if they capture an iterator variable in a loop. var actions = new List<Action>(); foreach (int i in new[] { 1, 2 }) actions.Add(() => Console.WriteLine(i)); for (int i = 3; i <= 4; i++) actions.Add(() => Console.WriteLine(i)); foreach (var action in actions) action(); The above code produces a strange result (I'm using .NET 4.5 compiler): 1 2 5 5 Why is the value of i captured differently for 2 almost

Why does printing the owner of a closure in a string cause infinite recursion?

时光总嘲笑我的痴心妄想 提交于 2019-12-01 18:24:14
I'm playing with closures and seeing this odd behavior that I can't quite explain: groovy:000> ({ println owner })() groovysh_evaluate@200b6145 ===> null groovy:000> ({ println "${owner}" })() groovysh_evaluate@2bf75a70 ===> null groovy:000> ({ ({ println owner })() })() groovysh_evaluate$_run_closure1@10f67a01 ===> null groovy:000> ({ ({ println "${owner}" })() })() ERROR java.lang.StackOverflowError: null at groovysh_evaluate$_run_closure1_closure2.doCall (groovysh_evaluate:2) at groovysh_evaluate$_run_closure1_closure2.doCall (groovysh_evaluate) at groovysh_evaluate$_run_closure1.doCall

JavaScript closure and the this object

点点圈 提交于 2019-12-01 18:04:46
I thought I had a reasonable understanding of the this object in JavaScript. When dealing with objects, callbacks, and both events and handlers, I haven't had a problem with it since time immemorial. Now, however, all has changed. I've fallen head over heels in love with JavaScript. Pure JS, that is, not jQuery, prototype.js, dojo... So naturally, I've taken to using closures. In some cases, though, this is catching me off guard here. Take this snippet for one: function anyFunc(par) { //console.log(par); console.log(this); } function makeClosure(func) { return function(par) { return func(par);

PHP Closures scoping of variables

↘锁芯ラ 提交于 2019-12-01 17:57:55
I am looking at the PHP example of Closures on http://us1.php.net/manual/en/functions.anonymous.php It provides the example code below and states: Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header. Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from). See the following

Repetitive try-catch blocks with Groovy 'with' closure?

孤者浪人 提交于 2019-12-01 17:48:33
I have the following Groovy class: @Slf4j class WidgetService { WidgetDao widgetDao = new WidgetDao() createWidget(String name, int type) { try { widgetDao.createWidget(name, type) } catch(WidgetException wexc) { log.error(wexc) int x = doFizz() long y = doBuzz(x) determineHowToHandle(y) } } Widget getWidgetById(Long id) { try { widgetDao.getWidgetById(id) } catch(WidgetException wexc) { log.error(wexc) int x = doFizz() long y = doBuzz(x) determineHowToHandle(y) } } Widget getWidgetByName(String name) { try { widgetDao.getWidgetByName(name) } catch(WidgetException wexc) { log.error(wexc) int x

Passing parameters into a closure for setTimeout

↘锁芯ラ 提交于 2019-12-01 17:47:04
I've run into an issue where my app lives in an iframe and it's being called from an external domain. IE9 won't fire the load event when the iframe loads properly so I think I'm stuck using setTimeout to poll the page. Anyway, I want to see what duration is generally needed for my setTimeout to complete, so I wanted to be able to log the delay the setTimeout fires from my callback, but I'm not sure how to pass that context into it so I can log it. App.readyIE9 = function() { var timings = [1,250,500,750,1000,1500,2000,3000]; for(var i = 0; i < timings.length; i++) { var func = function() { if

PHP Closures scoping of variables

风流意气都作罢 提交于 2019-12-01 17:32:31
问题 I am looking at the PHP example of Closures on http://us1.php.net/manual/en/functions.anonymous.php It provides the example code below and states: Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header. Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function