0

For the following code, s becomes empty after assignment. I treat s as a global variable for usage within the anonymous function. But seems like it is now. How is it work?

 var s = "";

    Utility.readAllFiles('testth/expression', function (err, results) {
        if (err) throw err;
        console.log(results);
        s += results.join(",");//s is non-empty here
    });



    res.end(s + "\n\n\n");//s is empty here
william007
  • 17,375
  • 25
  • 118
  • 194
  • 1
    This is _asynchronous_, your `res.end(s ...` is triggered _before_ the call**back** of your `readAllFiles` is triggered, therefor the `s` is empty. Please search for asynchronous javascript, theres tons of questions on SO about it already. – somethinghere Nov 16 '15 at 11:24
  • Move `res.end(s...` statement inside the `readAllFiles` callback. – Tushar Nov 16 '15 at 11:24

0 Answers0