admin管理员组

文章数量:1402469

I am used to taking a users request, dealing with it, and responding with the oute.

However, I have an API endpoint that has about 10 things that need to happen to various databases, logging, emailing etc. All asynchronously. Only the first database call needs to succeed for the user to get a response. The rest can carry on.

The issue is that the entire process can take 10/20 seconds, so the user is waiting 10/20 seconds when they only need their answer after the first DB query (about 100 ms).

I basically need

  • User makes a request
  • an async db call happens
  • If that fails, it errors to user, but if it succeeds, it responds with a success
  • the user is free to carry on doing what they need but the server continues with a few other db calls

I am using async await for all the asynchronous calls and express as my web framework.

I am used to taking a users request, dealing with it, and responding with the oute.

However, I have an API endpoint that has about 10 things that need to happen to various databases, logging, emailing etc. All asynchronously. Only the first database call needs to succeed for the user to get a response. The rest can carry on.

The issue is that the entire process can take 10/20 seconds, so the user is waiting 10/20 seconds when they only need their answer after the first DB query (about 100 ms).

I basically need

  • User makes a request
  • an async db call happens
  • If that fails, it errors to user, but if it succeeds, it responds with a success
  • the user is free to carry on doing what they need but the server continues with a few other db calls

I am using async await for all the asynchronous calls and express as my web framework.

Share Improve this question asked Mar 27, 2018 at 10:18 KittenKillerKittenKiller 14710 bronze badges 4
  • 1 Can you post your code? – Dipak Commented Mar 27, 2018 at 10:20
  • 1 So, what did you try? – briosheje Commented Mar 27, 2018 at 10:20
  • 1 Without code it's hard to tell but if you're using 'await', each line will wait for the previous one before it executes, if you simply call the async function without 'await' it will execute asynchronously. – Michael Curry Commented Mar 27, 2018 at 10:22
  • 1 As far as I can remember, you can return a response and still continue working on the request. But it will be better if you either breakdown the endpoint or bring in queue or a scheduler which will do the other 9 things. One significance of sending a response at the end of all tasks is to make sure that all things are pleted or it broke at a particular step. But if you take that away from a request you might end up in an inconsistent state. If you bring in a queue or scheduler you can do the retry mechanism or other error handling stuffs which you can't do if you go by your approach. – lovubuntu Commented Mar 27, 2018 at 10:27
Add a ment  | 

1 Answer 1

Reset to default 11

since you stated "Only the first database call needs to succeed for the user to get a response. The rest can carry on." i think get what you need already, so lets say you are using async await, lets do it this way:

async function requestHandler(req){
    await result = firstDatabaseRequestThatIsImportant();
    setOfAsyncFunctions(result);
    return result;
}

async function setOfAsyncFunctions(result){
    asyncFunction1(?????)
    asyncFunction2(?????)
}

So basically dont use await on the setOfAsyncFunctions so that this function will run in different thread and you can return the result right away to user. Maybe this will help you with your problem statements?

本文标签: javascriptNode and Express Return to user and continue executionStack Overflow