1

How can I create an asynchronous function in native Javascript without using setTimeout, setImmediate or process.nextTick?

3
  • use promise / deferred npmjs.com/package/promise or github.com/kriskowal/q Commented May 21, 2015 at 6:58
  • 2
    @TommyDDD 'Regular' functions in javascript are not async by default by any means - OP might wanna take a peak at this though: developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/… Commented May 21, 2015 at 7:04
  • @kwan245 promises are not async. node.js runs on a single thread. in order for something to be truly async you have to start another process (hopefully on a different core than the one running your main node.js process). that's what I am asking about. Commented Jun 8, 2015 at 6:11

1 Answer 1

2

I'm assuming you mean concurrent code and not async.

The closer you can get to concurrent code in JavaScript without faking it using timers is by using the WebWorker API in the browser, or child_process in Node.js.

Web Workers provide a simple means for web content to run scripts in background threads.

Any code running in a WebWorker is independent of the 'main' thread - thus running concurrently in a sense.

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

4 Comments

Good answer. And before using check browser compatibility list at bottom of reference.
I was wondering about node.js, not the browser, but this helps anyway
@AlexMills I'm pretty sure it applies on node.js as well
Yes, but I think the child_process (nodejs.org/api/child_process.html) core module in Node.js is more applicable than Web Workers; I might change the accepted answer if I get a better one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.