Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? [duplicate]

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, variable will 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 variable to 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.

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?

Best, and thanks,
Sami

like image 435
thisissami Avatar asked Aug 13 '11 23:08

thisissami


People also ask

Can I return value from callback function JavaScript?

A callback function can return a value, in other words, but the code that calls the function won't pay attention to the return value. Yes, it makes fun sense to try and return a value from a promise callback.

How do callbacks work in JavaScript?

A JavaScript callback is a function which is to be executed after another function has finished execution. A more formal definition would be - Any function that is passed as an argument to another function so that it can be executed in that other function is called as a callback function.

How do you make a callback asynchronous?

Callbacks are not asynchronous by nature, but can be used for asynchronous purposes. In this code, you define a function fn , define a function higherOrderFunction that takes a function callback as an argument, and pass fn as a callback to higherOrderFunction .

What is synchronous callback in JavaScript?

The synchronous callback The synchronous callback is executed during the execution of the higher-order function that uses the callback. In other words, the synchronous callbacks are blocking: the higher-order function doesn't complete its execution until the callback is done executing.


1 Answers

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 v and not the outside variable. To make this read much better I would suggest extracting the constructor of the callback as a named function.

function callbackFor(v) {   return function(err) { /* something with v */ }; } for(var i = 0; i < length; i++) {   var variable = variables[i];   otherVariable.doSomething(callbackFor(variable)); } 
like image 119
aparker42 Avatar answered Sep 27 '22 02:09

aparker42