Mathew Jenkinson
Mathew Jenkinson

Reputation: 854

NodeJS Express Multi Async on complete

Im trying to write a better JS statement than lots of nested .then()'s.

I have an endpoint that has lots of configurable options based on an imported config file.

eg: write to DB, do a language translation, do XYZ task.

Right now, its setup as lots of nested thens eg:

do TaskA -> then-> do TaskB -> then-> do TaskN

Im trying to move to a model where its more like

Do TaskA
and
Do TaskB
and 
Do TaskN

Once all the tasks have been completed then return the response back to the user.

In my head the JS looks like this:

// 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);

});

Do I do this with lots of promises or is there another way to achieve this? Thanks

Upvotes: 0

Views: 155

Answers (1)

shiva2492
shiva2492

Reputation: 429

So, you could use async/await, If you are trying to avoid lots of .then promises,

Somthing like below,


// 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);

});



Upvotes: 1

Related Questions