Executing functions in a loop in javascript -
Executing functions in a loop in javascript -
i'm missing in java script. i'd execute same function different parameters sequentially in loop (like in commented part of code). i'm instead using callback, guess it's not nicest way, , not flexible, illustration if had more paths looped over. cleanest approach in sewuentially executing functions?
var info = ''; var filepath = ['path1', 'path2']; somefunction(filepath, callback) { //dosth (); callback(); } //filepath = ['path1', 'path2']; //for ( var = 0; < filepath.length; = + 1 ) { // somefunction( filepath[i] ); //} somefunction( filepath[0] , function() { console.log("finished processing file 1"); countfromfile( filepath[1], function() { console.log("finished processing file 2"); savetofile( info ); }); });
use async create foreach asynchronous function.
example:
filepath = ['path1', 'path2']; async.each(filepath, somefunction, function(err){ // if of saves produced error, err equal error });
javascript
Comments
Post a Comment