I am using Vert.x after working with Node.js for a few years and am trying to replicate some of the functionality we'd see in require('async')
package huru.util;
import java.util.ArrayList;
import java.util.List;
interface AsyncCallback {
public void cb(Object e, Object v);
}
interface AsyncTask {
public void run(AsyncCallback cb);
}
interface FinalCallback {
public void run(Object e, List<Object> v);
}
public class Async {
static void Parallel (List<AsyncTask> tasks, FinalCallback f) {
List<Object> results = new ArrayList<Object>();
int count = 0;
for(int i = 0; i < tasks.size(); i++){
tasks.get(i).run((e,v) -> {
if(e != null){
f.run(e, null);
return;
}
count++; // ! here, count needs to be final ??
results.set(i,v);
if(count == tasks.size()){
f.run(null, results);
}
});
}
}
}
the above is getting there, but I get this error:
Variable used in lambda expression should be final or effectively final
I assume it's a similar problem that we see in JS where the value of i might change as the loop continues.
Anyone know how to mitigate this one? Wrapping it in a self-invoking function would be how to do this with JS.