Azevedo Azevedo - 1 month ago 18
Node.js Question

Handling multiple child processes in node.js

I have this download manager that will donwload files from

queue[]
in a
wget
separated child process each.

However, after the download is complete and the callback is called I need to handle
queue[i]
but I don't know which queue index is it.

const EXEC_FILE = require('child_process').execFile; /** https://nodejs.org/api/child_process.html */

queue = []; /** array with download urls */

function startDownload(i){
var downloadProccess = EXEC_FILE(
wget, [ queue[i] ],
(error, stdout, stderr) => {

/** callback after EXEC_FILE completed or aborted */
/** HERE! How do I get the index i? */

});
}

startDownload(1);
startDownload(3);
startDownload(9);


How do I get the
i
value after the process is complete?

Answer

Javascript functions are closures. So you already have the variable i inside the function body.