使用async.js时如何处理coffeescript中的隐式返回

使用async.js时如何处理coffeescript中的隐式返回,coffeescript,async.js,Coffeescript,Async.js,我正试着去 testFunction: () -> console.log "testFunction" async.series( (-> console.log "first" ), (-> console.log "second" ) ) 我也尝试过,但没有成功 testFunction: () -> console.log "testFunction" async.series(

我正试着去

testFunction: () ->
  console.log "testFunction"
  async.series(
    (->
      console.log "first"
    ),
    (-> 
      console.log "second"
    )
  )
我也尝试过,但没有成功

testFunction: () ->
  console.log "testFunction"
  async.series(
    (->
      console.log "first"
      return undefined
    ),
    (-> 
      console.log "second"
      return undefined
    )
  )
要运行,我希望控制台输出为“testFunction”、“first”、“second”,但我得到的是“testFunction”、“second”,我想coffeescript使用隐式返回似乎有问题

附件是从上述coffeescript编译的javascript输出的屏幕截图


每个用于异步的函数都需要将回调作为其唯一参数

one = (callback) ->
  console.log "one"
  callback()

two = (callback) ->
  console.log "two"
  callback()

testFunction: () ->
  console.log "testFunction"
  async.series [one, two], (error) ->
    console.log "all done", error

每个为异步工作的函数都需要将回调作为其唯一参数

one = (callback) ->
  console.log "one"
  callback()

two = (callback) ->
  console.log "two"
  callback()

testFunction: () ->
  console.log "testFunction"
  async.series [one, two], (error) ->
    console.log "all done", error

你有很多问题。首先,您没有将正确的参数传递给。它期望:

async.series([functions...], callback)
你打电话的时候

async.series(function, function)
由于第一个函数的
length
属性未定义,因此它假定其为空数组,并直接跳到“callback”(第二个函数)。听起来您可能希望传递一个包含两个函数的数组,而忽略回调

第二个问题是,传递给
async.series
的函数必须调用回调才能继续前进。回调是每个函数的第一个参数:

testFunction: () ->
  console.log "testFunction"
  async.series([
    ((next) ->
      console.log "first"
      next()
    ),
    ((next) -> 
      console.log "second"
      next()
    )
  ])

async
忽略传递给它的大多数(所有?)函数的返回值。

您遇到了许多问题。首先,您没有将正确的参数传递给。它期望:

async.series([functions...], callback)
你打电话的时候

async.series(function, function)
由于第一个函数的
length
属性未定义,因此它假定其为空数组,并直接跳到“callback”(第二个函数)。听起来您可能希望传递一个包含两个函数的数组,而忽略回调

第二个问题是,传递给
async.series
的函数必须调用回调才能继续前进。回调是每个函数的第一个参数:

testFunction: () ->
  console.log "testFunction"
  async.series([
    ((next) ->
      console.log "first"
      next()
    ),
    ((next) -> 
      console.log "second"
      next()
    )
  ])

async
忽略传递给它的大多数(所有?)函数的返回值。

这是一个非常有用的答案,谢谢。这就是我感到沮丧的地方,我已经看了一段文档了,但不清楚我是否需要达到这个目标。如果你不能传入参数,那么如何在闭包参数中给出函数呢?所以故事有点复杂,但我想保持简单。从技术上讲,您可以使用async.apply并接受几个参数,但最后一个参数始终是告诉async“我完成了”的回调。这是一个非常有用的答案,谢谢。这就是我感到沮丧的地方,我已经看了一段文档了,但不清楚我是否需要达到这个目标。如果你不能传入参数,那么如何在闭包参数中给出函数呢?所以故事有点复杂,但我想保持简单。从技术上讲,您可以使用async.apply并接受多个参数,但最后一个参数始终是告诉async“我完成了”的回调。