简体   繁体   English

将回调函数加入单个回调的最佳方法是什么?

[英]What is the best way to join callback functions into a single callback?

Given the JavaScript code: 给定JavaScript代码:

self.queue.now( 'test_1', function() {
    self.queue.now( 'test_2', done )
})

.. where the now fn() has a callback argument, what is the best way to combine / join the callbacks so that the code is no longer nested? .. now fn()具有回调参数的地方,组合/加入回调以使代码不再嵌套的最佳方法是什么? Specifically, I would like to have the self.queue.now calls be on the same indentation level and when both are finished, only then, would done() be called. 具体来说,我想让self.queue.now调用具有相同的缩进级别,并且当两者都完成时,只有那时,才会调用done()

Solution ended up leveraging async.parallel: 解决方案最终利用async.parallel:

async.parallel( [
    function( cb ) {
        self.queue.now( 'test_1', cb )
    },
    function( cb ) {
        self.queue.now( 'test_2', cb )
    }
], done )

Although it would have been nice to not have the function( cb ) wrappers .. 尽管没有function( cb )包装器会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM