0

我正在尝试将参数传递给异步节点模块所需的迭代器函数。

async.forEach dbReply, mediaHandler(entry, event.folder, callback), (error) ->
   console.log error 

mediaHandler = (entry, folder, callback) ->
   console.log arguments

我经常得到 ReferenceError: entry is not defined

关于如何将 event.folder 参数传递给函数的任何线索?

4

1 回答 1

0

Sounds like you want your mediaHandler function to return an iterator function:

mediaHandler = (folder, callback) ->
    (entry) ->
        # Do things with folder, callback, and entry.
        # folder and callback are available through the closure,
        # entry is supplied by forEach.

and then:

async.forEach dbReply, mediaHandler(event.folder, callback), (error) -> ...

Your entry argument is supplied by forEach when it calls the iterator but the other two arguments are (presumably) available when you're calling async.forEach so you're trying to curry a three argument mediaHandler to get a one argument function that forEach can deal with; there are other ways to do that in (Coffee|Java)Script but doing it by hand with a closure is probably the simplest.

于 2012-12-29T22:27:08.843 回答