Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7811447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:09:56+00:00 2026-06-02T04:09:56+00:00

I want to add some admin utilities to a little Web app, such as

  • 0

I want to add some admin utilities to a little Web app, such as “Backup Database”. The user will click on a button and the HTTP response will return immediately, although the potentially long-running process has been started in the background.

In Java this would probably be implemented by spawning an independent thread, in Scala by using an Actor. But what’s an appropriate idiom in node.js? (code snippet appreciated)

I’m now re-reading the docs, this really does seem a node 101 question but that’s pretty much where I am on this…anyhow, to clarify this is the basic scenario :

function onRequest(request, response) {
    doSomething();
    response.writeHead(202, headers);
    response.end("doing something");
}

function doSomething(){
     // long-running operation
}

I want the response to return immediately, leaving doSomething() running in the background.

Ok, given the single-thread model of node that doesn’t seem possible without spawning another OS-level ChildProcess. My misunderstanding.

In my code what I need for backup is mostly I/O based, so node should handle that in a nice async fashion. What I think I’ll do is shift the doSomething to after the response.end, see how that behaves.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:09:58+00:00Added an answer on June 2, 2026 at 4:09 am

    I don’t see the problem. All you need to do is have doSomething() start an asynchronous operation. It’ll return immediately, your onRequest will write the response back, and the client will get their “OK, I started” message.

    function doSomething() {
        openDatabaseConnection(connectionString, function(conn) {
            // This is called some time later, once the connection is established.
            // Now you can tell the database to back itself up.
        });
    }
    

    doSomething won’t just sit there until the database connection is established, or wait while you tell it to back up. It’ll return right away, having registered a callback that will run later. Behind the scenes, your database library is probably creating some threads for you, to make the async work the way it should, but your code doesn’t need to worry about it; you just return right away, send the response to the client right away, and the asynchronous code keeps running asynchronously.

    (It’s actually more work to make this run synchronously — you would have to pass your response object into doSomething, and have doSomething do the response.end call inside the innermost callback, after the backup is done. Of course, that’s not what you want to do here; you want to return immediately, which is exactly what your code will do.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add some html element (button, a tag, etc ) to a
I added some models in my models.py and I want to add an admin
I want to add some settings to the Django admin interface. In particular I
I'm building an admin panel for some website, and I want to add the
I want to add some functionality in admin that allows me to upload file
I want to add some custom fields to each Wordpress User Profile. I am
I have a website built with ASP.NET (3.5) and want add some level of
I want to add some more library and helpers file into Pyrocms which is
I want to add some no. of days to get the future date. And
I want to add some jQuery functionality to our sites where one piece of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.