I have just started programming with JS and Node, and I haven't got used to asynchronous stuff yet. Basically, i have the following code:
for (var i=0, len=sources.length; i<len; i++) {
processSource(sources[i], function(info) {
doesOtherStuff(sources[i], info);
});
}
It doesn't really work, because, as processSource
takes a while to finish, the function doesOtherStuff
is called with unmatching arguments, like sources[2]
and the processed info for sources[0]
.
What is the correct way of dealing with this? Is there something inherently wrong with the design of these functions. (both processSource and doesOtherStuff are my functions).