0

I have a question that if i want to make the for loop be sync.

because now my code will excute when for loop is done.

it will cause my function just work one time. Please tell me how can i edit it!

Code

output

  • Possible duplicate of [Whats the smartest / cleanest way to iterate async over arrays (or objs)?](http://stackoverflow.com/questions/8413857/whats-the-smartest-cleanest-way-to-iterate-async-over-arrays-or-objs) – cviejo Jul 01 '16 at 13:01
  • Take a look at [async](https://github.com/caolan/async) or promises. Or any of the existing questions on SO about this. Remember to always check if your question has already been asked before. Also, please paste the code itself and not an image of it. Makes it easier for others to run your code and replicate the issue – cviejo Jul 01 '16 at 13:02

1 Answers1

0

Have a look at the async Library http://caolan.github.io/async/docs.html#.each

async.each(openFiles, function(file, callback) {

    // Perform operation on file here.
    console.log('Processing file ' + file);

     callback();

}, function(err) {

   if(!err){
     //because now my code will execute when for loop is done.
     //code here will run only after loop is done
    }

});
Ratan Kumar
  • 1,592
  • 3
  • 23
  • 49