I am working on an app that will run queries and return results to UI. However, these queries can take a long time to finish so I should not wait for the result. As far as I know, people use task queues in this case.

My question is when task queue returns the result, how do I send the result to UI since the http request is already finished by that point?

EDIT: I will be using React and NodeJS

  • 3
    the UI either polls or establishes a socket connection and listens for messages
    – Ewan
    Dec 7 '18 at 15:31

AJAX will be your friend here.

You'll need to take the user to a page that will display the results of the query, but set up a polling mechanism that asks the server if the task is complete yet.

Meanwhile, the query is executing in the background. Once the results come back, generate the result, either as objects stored in the session for the user, or serialize them as JSON, XML or HTML. Instead of the user's session, it could just be a general purpose cache whose key is generated when the user submits the query (using a GUID maybe?).

The browser pings the server (with the query Id) until the server detects that the cache key exists, and returns the results back to the browser.


Consider using a reactive design library for your UI, if you don't have one already.

Then, as Greg mentioned, you can apply AJAX techniques to asynchronously update your UI when the query is complete and the results are ready. If, for example, you are using ReactJS, they provide a relatively straightforward example of the mechanism here. That page also names and links three libraries for AJAX that you could use: Axios, jQuery AJAX, and window.fetch.

Without knowing more about what framework libraries you have chosen to use on your project, it's impossible to give more specific advice.

  • In AngularJs, it'd be promises, in TypeScript using RxJs: observables. Using jQuery, it'd be jQuery Ajax, etc...
    – RandomUs1r
    Sep 3 '19 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.