Javascript NodeJS在完成时表示多异步

Javascript NodeJS在完成时表示多异步,javascript,node.js,express,Javascript,Node.js,Express,我试图写一个比很多嵌套的.then()更好的JS语句 我有一个端点,它有许多基于导入的配置文件的可配置选项 例如:写DB,做语言翻译,做XYZ任务 现在,它的设置为许多嵌套的,例如: do TaskA -> then-> do TaskB -> then-> do TaskN 我想换一个更像 Do TaskA and Do TaskB and Do TaskN 完成所有任务后,将响应返回给用户 在我看来,JS是这样的: // Example Multi Async

我试图写一个比很多嵌套的.then()更好的JS语句

我有一个端点,它有许多基于导入的配置文件的可配置选项

例如:写DB,做语言翻译,做XYZ任务

现在,它的设置为许多嵌套的,例如:

do TaskA -> then-> do TaskB -> then-> do TaskN
我想换一个更像

Do TaskA
and
Do TaskB
and 
Do TaskN
完成所有任务后,将响应返回给用户

在我看来,JS是这样的:

// Example Multi Async Job
app.post('/command/:commandType', (request, response) => {
    var responseJSON = {};
    responseJSON.status = 500;
    responseJSON.success = false;
    responseJSON.message = "I failed to do all the things.";

    // If needed do language translation
    if(config.languageTranslation == true){
        async do the language translation 
    }

    // If needed write to DB
    if(config.storeDB == true){
        async write to the DB
    }

    // If needed do XYZ task
    if(config.xyz == true){
        async do the thing
    }

    // On success of all the tasks, update the responseJSON
    responseJSON.status = 200;
    responseJSON.success = true;
    responseJSON.message = "I did all the things";

    // If any of the jobs fail, then the fallback is the 500 and success==false from the beginning
    response.status(responseJSON.status).json(responseJSON);

});
我是通过许许多多的承诺来做到这一点,还是有其他方法来实现这一点?
谢谢

所以,如果您试图避免大量的。那么,您可以使用async/wait

像下面这样的事情


// Example Multi Async Job
app.post('/command/:commandType', async (request, response) => {
    var responseJSON = {};
    responseJSON.status = 500;
    responseJSON.success = false;
    responseJSON.message = "I failed to do all the things.";

    // If needed do language translation
    if(config.languageTranslation == true){
        // If the below method returns a promise then await 
        // else call without await
        await languageTranslation()
    }

    // If needed write to DB
    if(config.storeDB == true){
        // call you db native or ORM module insert or update( depending on your situation )
        await db.insert(req.body)
    }

    // If needed do XYZ task
    if(config.xyz == true){
        // If the below method returns a promise then await 
        // else call without await
        await xyzTask()
    }

    // On success of all the tasks, update the responseJSON
    responseJSON.status = 200;
    responseJSON.success = true;
    responseJSON.message = "I did all the things";

    // If any of the jobs fail, then the fallback is the 500 and success==false from the beginning
    response.status(responseJSON.status).json(responseJSON);

});




这三件事需要顺序吗?或者它们可以并行运行?它们都可以并行运行,TaskB不依赖于TaskA。
async
库为您提供了一些非常好的帮助。case
parallel
函数将完成此任务,因为不清楚您等待的是否是languageTranslation()和xyzTask()的承诺。这些应该首先得到保证。他问如何才能“执行任务A->then->do TaskB->then->do TaskN”,意思是每个任务都是相互依赖的,在他的代码中,他写的都是异步调用,那么我看不出投反对票的原因。因为这个问题的答案已经足够了,所以他没有问他的方法是基于回调的,并且需要promisify转化为返回承诺。