0

In node.js, how do you call a function so it runs in background? Something like:

work = function(){
    for (var i=0; i<1000000; ++i);
    return "world!";
};

spawn(work).then(console.log);
console.log("Hello ");

Should output what you expect.

Note: not necessarily following this pattern.

10
  • Are you asking for a new thread, or just an asynchronous execution? Separate threads are problematic because they can't share variables and thus they can't be created from an existing function. Commented May 26, 2013 at 5:01
  • You mean separate processes? Commented May 26, 2013 at 5:35
  • 1
    Threads, on the OS level and in most languages, share the memory space and can share variables. This causes its own set of issues in those languages, mainly when programmers underestimate the effects of concurrency or when they forget to tell the compiler to avoid a certain set of optimisations (caching and write reordering) that are valid in single-threaded environment. Javascript solves that problem by disallowing concurrency altogether - no event handler is run before the previous one finishes. When concurrency is added to javascript, it must be done by passing messages between the thr... Commented May 26, 2013 at 6:08
  • 1
    ...eads, and no variables can be shared. They are still called threads because they are assumed to share the memory space on the implementation level (despite that this fact must be hidden from the proggramer) and because they are still logically a part of the same application. Commented May 26, 2013 at 6:10
  • 1
    Actually, they are called processes on Node.js, and web workers in the browser environment. nodejs.org/api/child_process.html Commented May 26, 2013 at 6:14

3 Answers 3

1

Nothing in Node.JS will run "in the background". This is because JS can't multi-thread. Yet it has the ability to run code back to back, for example running 2 for loops at the same time, will cause the first for loop to iterate a set amount, then the second will iterate and they will swap processing power to make it seem as if methods can be run at the same time.

Node.JS if I am not mistaken does this with the callbacks.

"Callbacks

Callbacks are a basic idiom in node.js for asynchronous operations. When most people talk about callbacks, they mean the a function that is passed as the last parameter to an asynchronous function. The callback is then later called with any return value or error message that the function produced. For more details, see the article on callbacks"

With more example and information found here -

http://docs.nodejitsu.com/articles/getting-started/control-flow/how-to-write-asynchronous-code

Sign up to request clarification or add additional context in comments.

Comments

0

Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript https://npmjs.org/package/async

Comments

0

Try looking at the child_process features.

I'm currently using child_process to fork processes and parallelize operations. The best part is that (unlike working in C or C++), node does a lot of the painful work for you.

Even more (a side note), you can pass JavaScript code back and forth between processes and build a powerful multi-CPU, multi-host and multi-process application for compute-intensive tasks. Don't let the negative voices tell you otherwise...node is great and it can do what you appear to be asking.

Check out http://nodejs.org/api/child_process.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.