9
Thanks in advance for taking a look at this.
I've got an async task within a loop that's not working. I've made sure to:
- Wrap my looping variable "key" in a closure to avoid the classic "last value only" problem, where the loop finishes long before the async tasks returns a value, and only the last value is displayed.
- Call done(error) to complete my async task (per the Gruntjs FAQ)
- Use hasOwnProperty() to make sure that the key is an actual property of an object, and doesn't come from the prototype.
- Follow the format of the node.exec example, with the exception of assigning the value of exec to a variable--which I've tried, but it didn't help. See first reference below.
This function outputs... nothing!? For some reason the grunt.log.writeln statements aren't even firing. Task completes without errors. I also tried adding a 20-second delay, in case the script was finishing before the async task were returned. Oddly, if I don't call "done(error)," the files are written to file (when I replace the writeln's with grunt.file.write statements).
var done = this.async(),
exec = require('child_process').exec,
myJSON = {
"file1" : "C:/home/me/jquery.js",
"file2 " : "C:/home/me/grunt.js",
...
"fileN" : "C:/home/me/N.js"
},
count;
for (var key in myJSON) {
if (myJSON.hasOwnProperty(key)) {
(function (key) {
exec( 'type "' + myJSON[key] + '"',
function(error, stdout, stderr) {
if (error) {
grunt.log.writeln('!!! exec error: ' + error);
}
grunt.log.writeln('stdout: ' + stdout);
grunt.log.writeln('stderr: ' + stderr);
done(error);
}
);
})(key);
count++;
}
}
References: