node.js callback in a for-loop -
im trying read data , put multi-array. open different files use for-loop, because need results numbered in correct order. problem:
i cant save data in 1 array. once call function read data, can use result inside function borders. how can make whole array available outside function? (pretty sure messing callbacks).
in other words: trying open multiple files , save content in 1 array [[file1], [file2], ...].
here code:
var einlesen = require ('./einlesen/einlesen.js'); var einzeln = function(arg, callback){ //this function use read data (it works fine, checked separately) einlesen.einlesen(arg[0], arg[1], arg[2], function(err, output){ //this function opens file, works on content , returns array containing needed data if (err) { return cb1(err); } callback(output); }); } var daten_einlesen = function (callback){ var tabellenarray = []; //initiating array in whole info should go var tabelleninfos = [ //this array contains info data should read, need know there 16 files read, rest not important ['gvz', '<|>', '1,1,1,0,1,0,0,0,1'], ['gzv', ';', '1,1,1,1,1,1,1,1'], ['xpg', ';', '1,1,1,1,1'], ['abf', ';', '1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1'], ['gus', '<|>', '1,1,0,1,0,0,0,0,1,1,1'], ['xpa', '<|>', '1,1,1'], ['alw', '<|>', '1,1,1'], ['tal', '<|>', '1,1,1'], ['alb', '<|>', '1,1,0,1,1,1,1,1,1'], ['zlb', '<|>', '1,1,1'], ['xaw', '<|>', '1,1,1'], ['sws', ';', '1,1,1,1,1,1,1,1,1,1'], ['pro', '<|>', '1,1'], ['vbp', '<|>', '1,0,1'], ['zvp', '<|>', '1,1,1'], ['config', ';', '0,0,1']]; (var = 0; < 3; i++){ //this loop try open files 1 one , input them "tabellenarray" einzeln(tabelleninfos[i], function(tabelle){ tabellenarray[i] = tabelle; //data saved array lose info onto next step of loop, if put final callback (below) in here loop doesnt work anymore (naturally) }); } callback(tabellenarray); } daten_einlesen(function(piep){ //calling whole function console.log(piep); });
i grateful advice.
nils
try starters:
var pending = 3,complete=0; (var = 0; < pending; i++){ //this loop try open files 1 one , input them "tabellenarray" einzeln(tabelleninfos[i], function(tabelle){ tabellenarray[i] = tabelle; //data saved array lose info onto next step of loop, if put final callback (below) in here loop doesnt work anymore (naturally) if(++complete >= pending){ callback(tabellenarray); } }); }
Comments
Post a Comment