简体   繁体   中英

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

Given the JavaScript code:

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? 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.

Solution ended up leveraging 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 ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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