“Spawn a thread”-like behaviour in node.js

综合技术 2018-02-14

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) {
    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.

Problem courtesy of: danja


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.

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.)

Solution courtesy of: Joe White

Node.js Recipes

责编内容by:Node.js Recipes (源链)。感谢您的支持!


Data Redaction and Virtual Columns Prior to 12.2, data redaction was unsafe if used in conjunction with virtual columns. In fact, it...
Planning, scaling and optimizing a large... I'm currently designing and developing a web application that has the potential to grow very large a...
The Evolution of the DBA in an “As-A-Service” Worl... The requirements for managing and running a database in a modern enterprise have evolved over the pa...
ModernJS – Yeoman Generator for Node.js with Slack... https://npmjs.org/package/generator-modern-js https://david-dm.org/georgschlenkhoff/generator-...
A Fare Cache in a Sharded Data Cluster Hipmunk is all about discovering a better travel experience, and that requires knowing the altern...