How to run Generator Functions in Parallel?

前端 未结 2 1003
囚心锁ツ
囚心锁ツ 2020-12-21 23:56

Assuming I have a Koa web server with an endpoint like this:

const perform = require(...); // some generator function

exports.endpoint = function* () {

            


        
2条回答
  •  一个人的身影
    2020-12-22 00:32

    co turns the generator function to Promises, and executes them async. Promise.all waits for all of them to finish:

    exports.getResults = function* () {
    
        var actions = [...];
    
        return yield Promise.all(actions.map(function(action) { 
            return co(function*() { 
                return yield perform(action); 
            } 
        }));
    }
    

提交回复
热议问题