如何使用Node.js嵌套asynchronousforEachSeries循环

使用coffeescript代码 一个解决scheme| 看到这个职位

我有两个循环。 我想从数组'a'中的每个值,然后循环所有的值'b'处理它们,并移动到数组'a'中的下一个值

预期产出:

1 abc 2 abc 3 abc 

错误我看到:

  [ 1, 2, 3 ] [ 'a', 'b', 'c' ] 1 2 3 TypeError: Cannot read property 'length' of undefined at Object.forEachSeries(~/src/node_modules/async/lib/async.js:103:17) Async = require('async') @a = [1,2,3] @b = ['a','b','c'] console.dir @a console.dir @b Async.forEachSeries @a, (aa , cbLoop1) -> console.log aa cbLoop1() Async.forEachSeries @b, (bb , cbLoop2) -> #here will be some callback that I need to process before moving to next value in #b array console.log bb cbLoop2() 

您的第一个Async.forEachSeries调用需要一个callback,它改变了这个值

 Async.forEachSeries @a, (aa , cbLoop1) -> # inside the callback, the value of `this` has changed, # so @b is undefined! 

使用=>语法在callback中保留这个值:

 Async.forEachSeries @a, (aa , cbLoop1) => console.log aa cbLoop1() Async.forEachSeries @b, (bb , cbLoop2) -> # use `=>` again for this callback if you need to access this (@) inside # this callback as well