1

I am lost in the callbacks. code and desired output is below. So what happening is inner loop is not executed that should prints the @b array=> ['a','b','c']

Async = require('async')

  @a = [1,2,3]
  @b = ['a','b','c']

  Async.forEachSeries @a, (aa , cbLoop1) =>
    console.log aa
    console.log "^ number from Loop-1"
    Async.forEachSeries @b, (bb , cbLoop2) =>
      #call the method below
      Async.waterfall(
          [
            (cb) ->
              #call method 'start'
              #'start' method has a callback that gets info using HTTP GET
              start bb , (error , response) ->
                  #console.log(response) or do something with response
              cbLoop2()
          ]    
      )
   cbLoop1()


   # Desired OUTPUT
   1
   ^ number from Loop-1
   a
   b
   c
   2
   ^ number from Loop-1
   a
   b
   c
   3
   ^ number from Loop-1
   a
   b
   c    
1

2 Answers 2

1

async.waterfall takes a 2nd argument: "An optional callback to run once all the functions have completed". It's unclear from your question if this breaks the flow you are trying to achieve, but could you just call cbLoop2() as the 2nd argument to waterfall instead of calling it at the end of the first task? A simplified example:

async = require('async')

a = [1,2,3]
b = ['a','b','c']

cb = ->

async.forEachSeries a, (item , cb) ->
  console.log item
  async.forEachSeries b, (item , cb) ->
    console.log item
    async.waterfall [], cb()
  cb()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for quick response. this is helpful , actually u are right the flow is broken if I have async.waterfall in between and thats what I am trying to do ... after all the steps in waterfall is achieved it should goes to next value in loop2 ..
0

*David answer helped me. I was trying get hold of Async forSeries and waterfall syntax. *any inputs to improve are welcome !

Async = require('async')

class Test
  constructor: () ->
    @a1       = [1,2,3]
    @b1       = ['a','b','c']

  test: (t , callback) ->
    Async.forEachSeries @a1, (aa , cbLoop1) =>
      console.log "Value from Array @a1 - > #{aa}"
      count = 0
      Async.forEachSeries @b1, (bb , cbLoop2) =>
    cb = cbLoop2
    # console.log bb
    Async.waterfall(
      [
        (cbLoop2) ->
          count = count + 1
          t.methd1 "Value from Array @b2 - #{bb}" , (er ,response) ->
            console.log response
            cbLoop2(null , count)
        , (resp , cbLoop2) ->
          #have to do some manupulations here
          cbLoop2(null , count)
      ] , (err ,response) ->
        cbLoop2()

    )

  , (err) ->
    console.log("===")
    cbLoop1()


  methd1: (data , callback) ->
    @finalmethod "$$ #{data} $$" , callback

  finalmethod: (data, callback) ->
    setTimeout () ->
      callback(undefined , data)
    , 1500


  t = new Test()
  t.test t, t.test_cb


output
Value from Array @a1 - > 1
$$ Value from Array @b2 - a $$
$$ Value from Array @b2 - b $$
$$ Value from Array @b2 - c $$
===
Value from Array @a1 - > 2
$$ Value from Array @b2 - a $$
$$ Value from Array @b2 - b $$
$$ Value from Array @b2 - c $$
===
Value from Array @a1 - > 3
$$ Value from Array @b2 - a $$
$$ Value from Array @b2 - b $$
$$ Value from Array @b2 - c $$
===

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.