在 javascript 中的循环内使用回调时,有没有办法保存在循环中更新的变量以供回调使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7053965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:37:41  来源:igfitidea点击:

When using callbacks inside a loop in javascript, is there any way to save a variable that's updated in the loop for use in the callback?

javascriptcallback

提问by thisissami

Let's say I have something as follows:

假设我有以下内容:

for(var i = 0; i < length; i++){
  var variable = variables[i];
  otherVariable.doSomething(variable, function(err){ //callback for when doSomething ends
    do something else with variable;
  }

By the time the callbacks are called, variablewill inevitably be the last variable for all the callbacks, instead of being a different one for each callback, as I would like. I realize that I could pass variableto doSomething()and then get that passed back as part of the callback, but doSomething()is part of an external library, and I'd rather not mess around with the source code for that.

到回调被调用时,variable将不可避免地成为所有回调的最后一个变量,而不是每个回调的不同变量,正如我所希望的。我意识到我可以传递variabledoSomething()然后将其作为回调的一部分传回,但它doSomething()是外部库的一部分,我宁愿不要为此弄乱源代码。

Do those of you that know JavaScript better than I do know if there are any alternative ways to do what I'd like to do?

你们中那些比我更了解 JavaScript 的人是否知道是否有其他方法可以做我想做的事情?

Best, and thanks,
Sami

最好的,谢谢,
萨米

采纳答案by aparker42

A common, if ugly, way of dealing with this situation is to use another function that is immediately invoked to create a scope to hold the variable.

处理这种情况的一种常见方法(如果很丑陋)是使用另一个函数,该函数立即被调用以创建一个作用域来保存变量。

for(var i = 0; i < length; i++) {
  var variable = variables[i];
  otherVariable.doSomething(function(v) { return function(err) { /* something with v */ }; }(variable));
}

Notice that inside the immediately invoked function the callback that is being created, and returned, references the parameter to the function vand not the outside variable. To make this read much better I would suggest extracting the constructor of the callback as a named function.

请注意,在立即调用的函数内部,正在创建和返回的回调将参数引用到函数v而不是外部variable。为了更好地阅读,我建议将回调的构造函数提取为命名函数。

function callbackFor(v) {
  return function(err) { /* something with v */ };
}
for(var i = 0; i < length; i++) {
  var variable = variables[i];
  otherVariable.doSomething(callbackFor(variable));
}

回答by thisissami

Ok I've seemed to figure this out. What I needed to do was to put a function(var)wrapper around otherVariable.doSomething(), so the updated code looks as follows:

好吧,我似乎想通了这一点。我需要做的是在function(var)周围放置一个包装器otherVariable.doSomething(),因此更新后的代码如下所示:

for(var i = 0; i < length; i++){
  var variable = variables[i];
  (function(var){ //start wrapper code
    otherVariable.doSomething(var, function(err){ //callback for when doSomething ends
      do something else with var; //please note that i'm dealing with var here, not variable
    }
  })(variable);//passing in variable to var here
}

hope this helps anybody else that gets stuck on something like this in the future!

希望这可以帮助其他任何人在未来陷入这样的事情!

@aparker42, i'd still love to hear your answer to my question in the comment to your question, since that still does confuse me.

@ aparker42,我仍然很想在对您的问题的评论中听到您对我的问题的回答,因为这仍然让我感到困惑。

EDIT: of course, since this is javascript, you wouldn't want to use varas a variable name.

编辑:当然,因为这是 javascript,你不会想var用作变量名。