Using coffeescript to code One solution | see this post
I have two loops. I want to take each value from array 'a' and then loop through all the values of 'b' process them and move to next value in array 'a'
output expected:
1 a b c
2 a b c
3 a b c
Error I see:
[ 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()
Your first Async.forEachSeries
call takes a callback, which changes the value of this
Async.forEachSeries @a, (aa , cbLoop1) ->
# inside the callback, the value of `this` has changed,
# so @b is undefined!
Use the =>
syntax to keep the value of this
inside the 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