Note that there are some explanatory texts on larger screens.

plurals
  1. POnode.js async parallel function returns a single result several times
    text
    copied!<p>I'm trying to upload several files in parallel using node.js's async module. My code looks like: </p> <pre><code>// fileArr is an array of objects. each object contains the attributes of the file to be uploaded - filename, path, destination path, etc. // toUpload is an array to which i push all the functions i want to execute with async.parallel for (i=0; i&lt;fileArr.length; i++) { var src = fileArr[i]['path'] var dest = fileArr[i]['dest'] var fn = function(cb) { self.upload(src, dest, headers, function(err, url) { cb(null, url) }) } toUpload.push(fn) } // for loop ends here async.parallel(toUpload, function(err, results) { console.log('results: ' + results) }) </code></pre> <p>My problem: for n = the number of functions in toUpload, the results array callback contains the result from the last parallel task in the array, n times. I can't figure this out. it seems like every function should return its own callback with (null, url) to the parallel function.</p> <p>also - when i try calling the self.upload function with the definitions of src and dest directly:</p> <pre><code>self.upload(fileArr[i]['path'], fileArr[i]['dest'], headers, function(err, url) { cb(null, url) }) </code></pre> <p>i get a error saying "cannot read property 'path' of undefined". so, fileArr[i] is undefined. why does this happen? i feel like there is some weirdness with assignments and scope going on...</p> <p>if it's not immediately obvious from the question (and the code), i'm pretty new to programming..</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload