[removed] execute a bunch of asynchronous method with one callback

前端 未结 4 1490
半阙折子戏
半阙折子戏 2020-12-01 08:21

I need to execute a bunch of asynchronous methods (client SQLite database), and call only one final callback.

Of course, the ugly way is:

execAll : f         


        
4条回答
  •  抹茶落季
    2020-12-01 08:58

    I've written some async utilities you might find useful, allowing you to write your example as:

    function(callback) {
        async.series([
            asynch1(),
            asynch2(),
            ...
            asynchN()
        ], callback);
    }
    

    Or, if you wanted to run them in parallel, as:

    function(callback) {
        async.parallel([
            asynch1(),
            asynch2(),
            ...
            asynchN()
        ], callback);
    }
    

    There are loads of other useful functions like async map/reduce too:

    http://caolanmcmahon.com/async.html

    Hope that helps!

提交回复
热议问题