How to use node exec synchronously?
<p>I have a situation where I need to execute exec synchronously. How can do it ? I found one lib execSync. But it is depreciated. So any other solution for this. Can I use bluebird promise for this ?</p>

<pre><code>   for (var i = 0; i &lt; testCasesLength; i++) {
        var currentTestCase = testCases[i];
        output.testCases.push({ passed: false, name: currentTestCase.name, input: currentTestCase.name, output: null });
        fs.writeFileSync(path + "/input" + i + ".txt", currentTestCase.input);
        var command = "cd " + path + " &amp; java Main &lt; input" + i + ".txt";
        exec(command, function(error, stdout, stderr) {
            if (error) {
                if (exports.stats) {
                    console.log('INFO: '.green + path + '/Main.java contained an error while executing');
                }
                if (error.toString().indexOf('Error: stdout maxBuffer exceeded.') != -1) {
                    output.error = 'Error: stdout maxBuffer exceeded. You might have initialized an infinite loop.';
                    fn(output);
                } else {
                    output.error = stderr;
                    fn(output);
                }
            } else {
                if (exports.stats) {
                    console.log('INFO: '.green + path + '/Main.java successfully compiled and executed !');
                }
                // On success test Running
                if (stdout == currentTestCase.output) {
                    output.testCases[i].passed = true;
                    output.score = output.score + parseInt(currentTestCase.score);
                } else {
                    output.testCases[i].output = stdout;
                }
            }
        });
    }
    fn(output);
</code></pre>