You need to think about how the loop blocks the stack. All code "block" , its just how you block that counts. All the threads in the world wont help you if you don't know how to break the stack. What nextTick does is break the stack, so it adds the function to the next run around nodes event loop. It allows other code to run in between as the loop cycles. Why i say mongoose/mongo is you may have a large array that you need to loop over. 1000+ items is not something you want to do all at once. break the loop up with nextTick.
Does this help? . On Friday, April 6, 2012 8:40:02 PM UTC-4, linxha wrote: > > On Fri, Apr 6, 2012 at 5:59 PM, Tim Price <[email protected]> wrote: > > What about this for an aync for loop > > > > var forLoop = function(data, worker, callBack) { > > if(Array.isArray(data)) { > > var returnData = []; > > var loop = function(i, cb) { > > if(i === data.length) { > > return callBack(null, returnData); > > } > > process.nextTick(function() { > > returnData.push(worker.call(data[i++])) > > loop(i); > > }); > > //console.log(i) > > } > > loop(0); > > } else { > > callBack(new Error('Not an array.')); > > } > > }; > > forLoop([{ > > name : 'sdsad' > > }, { > > name : 'sdssdfad' > > }, { > > name : 'sfdsad' > > }, { > > name : 'sdasad' > > }], function() { > > //worker function > > return this.name// return {firstName:this.name} > > }, function(err, result) { > > //work done function > > console.log(err, result) > > }); > > This is the part I hate, I don't understand this (yet). So I'll have > to go off an see if I can create some simple code to understand this. > > > Maybe could be used to traverse mongoose docs? > > ??? Not sure what this is for > -- > Neil Cherry > Linux Home Automation ( http://www.linuxha.com/ ) > Author: Linux Smart Homes For Dummies ( > http://linuxha.com/FD/book/index.html ) > > On Friday, April 6, 2012 8:40:02 PM UTC-4, linxha wrote: > > On Fri, Apr 6, 2012 at 5:59 PM, Tim Price <[email protected]> wrote: > > What about this for an aync for loop > > > > var forLoop = function(data, worker, callBack) { > > if(Array.isArray(data)) { > > var returnData = []; > > var loop = function(i, cb) { > > if(i === data.length) { > > return callBack(null, returnData); > > } > > process.nextTick(function() { > > returnData.push(worker.call(data[i++])) > > loop(i); > > }); > > //console.log(i) > > } > > loop(0); > > } else { > > callBack(new Error('Not an array.')); > > } > > }; > > forLoop([{ > > name : 'sdsad' > > }, { > > name : 'sdssdfad' > > }, { > > name : 'sfdsad' > > }, { > > name : 'sdasad' > > }], function() { > > //worker function > > return this.name// return {firstName:this.name} > > }, function(err, result) { > > //work done function > > console.log(err, result) > > }); > > This is the part I hate, I don't understand this (yet). So I'll have > to go off an see if I can create some simple code to understand this. > > > Maybe could be used to traverse mongoose docs? > > ??? Not sure what this is for > -- > Neil Cherry > Linux Home Automation ( http://www.linuxha.com/ ) > Author: Linux Smart Homes For Dummies ( > http://linuxha.com/FD/book/index.html ) > > On Friday, April 6, 2012 8:40:02 PM UTC-4, linxha wrote: > > On Fri, Apr 6, 2012 at 5:59 PM, Tim Price <[email protected]> wrote: > > What about this for an aync for loop > > > > var forLoop = function(data, worker, callBack) { > > if(Array.isArray(data)) { > > var returnData = []; > > var loop = function(i, cb) { > > if(i === data.length) { > > return callBack(null, returnData); > > } > > process.nextTick(function() { > > returnData.push(worker.call(data[i++])) > > loop(i); > > }); > > //console.log(i) > > } > > loop(0); > > } else { > > callBack(new Error('Not an array.')); > > } > > }; > > forLoop([{ > > name : 'sdsad' > > }, { > > name : 'sdssdfad' > > }, { > > name : 'sfdsad' > > }, { > > name : 'sdasad' > > }], function() { > > //worker function > > return this.name// return {firstName:this.name} > > }, function(err, result) { > > //work done function > > console.log(err, result) > > }); > > This is the part I hate, I don't understand this (yet). So I'll have > to go off an see if I can create some simple code to understand this. > > > Maybe could be used to traverse mongoose docs? > > ??? Not sure what this is for > -- > Neil Cherry > Linux Home Automation ( http://www.linuxha.com/ ) > Author: Linux Smart Homes For Dummies ( > http://linuxha.com/FD/book/index.html ) > > On Friday, April 6, 2012 8:40:02 PM UTC-4, linxha wrote: > > On Fri, Apr 6, 2012 at 5:59 PM, Tim Price <[email protected]> wrote: > > What about this for an aync for loop > > > > var forLoop = function(data, worker, callBack) { > > if(Array.isArray(data)) { > > var returnData = []; > > var loop = function(i, cb) { > > if(i === data.length) { > > return callBack(null, returnData); > > } > > process.nextTick(function() { > > returnData.push(worker.call(data[i++])) > > loop(i); > > }); > > //console.log(i) > > } > > loop(0); > > } else { > > callBack(new Error('Not an array.')); > > } > > }; > > forLoop([{ > > name : 'sdsad' > > }, { > > name : 'sdssdfad' > > }, { > > name : 'sfdsad' > > }, { > > name : 'sdasad' > > }], function() { > > //worker function > > return this.name// return {firstName:this.name} > > }, function(err, result) { > > //work done function > > console.log(err, result) > > }); > > This is the part I hate, I don't understand this (yet). So I'll have > to go off an see if I can create some simple code to understand this. > > > Maybe could be used to traverse mongoose docs? > > ??? Not sure what this is for > -- > Neil Cherry > Linux Home Automation ( http://www.linuxha.com/ ) > Author: Linux Smart Homes For Dummies ( > http://linuxha.com/FD/book/index.html ) > > On Friday, April 6, 2012 8:40:02 PM UTC-4, linxha wrote: > > On Fri, Apr 6, 2012 at 5:59 PM, Tim Price <[email protected]> wrote: > > What about this for an aync for loop > > > > var forLoop = function(data, worker, callBack) { > > if(Array.isArray(data)) { > > var returnData = []; > > var loop = function(i, cb) { > > if(i === data.length) { > > return callBack(null, returnData); > > } > > process.nextTick(function() { > > returnData.push(worker.call(data[i++])) > > loop(i); > > }); > > //console.log(i) > > } > > loop(0); > > } else { > > callBack(new Error('Not an array.')); > > } > > }; > > forLoop([{ > > name : 'sdsad' > > }, { > > name : 'sdssdfad' > > }, { > > name : 'sfdsad' > > }, { > > name : 'sdasad' > > }], function() { > > //worker function > > return this.name// return {firstName:this.name} > > }, function(err, result) { > > //work done function > > console.log(err, result) > > }); > > This is the part I hate, I don't understand this (yet). So I'll have > to go off an see if I can create some simple code to understand this. > > > Maybe could be used to traverse mongoose docs? > > ??? Not sure what this is for > -- > Neil Cherry > Linux Home Automation ( http://www.linuxha.com/ ) > Author: Linux Smart Homes For Dummies ( > http://linuxha.com/FD/book/index.html ) > > -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
