Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From your comment on Quentin's answer</p> <blockquote> <p>How to create a function which has some manipulation inside it which is a normal synchronous function but the overall flow doesn't wait for this function to complete rather moves to the next function.</p> </blockquote> <p>JavaScript, the language, doesn't have a feature to do that. So you look to the environment in which it's running to see if the environment has that feature.</p> <p>NodeJS does. In fact, it has several of them: <a href="http://nodejs.org/api/all.html#all_settimeout_callback_delay_arg" rel="nofollow noreferrer"><code>setTimeout</code></a> (which is a bit like what browsers give you), <a href="http://nodejs.org/api/all.html#all_setimmediate_callback_arg" rel="nofollow noreferrer"><code>setImmediate</code></a>, and <a href="http://nodejs.org/api/all.html#all_process_nexttick_callback" rel="nofollow noreferrer"><code>process.nextTick</code></a>. For your use case, <code>setImmediate</code> is probably the best choice, but <a href="https://stackoverflow.com/q/15349733/157247">see this question and its answers for more on that</a>. <code>setImmediate</code> looks like this:</p> <pre><code>setImmediate(functionToCall); </code></pre> <p>(It's a global, you don't have to <code>require</code> anything.)</p> <p>That does exactly what your title asks for: An asynchronous call to a synchronous function.</p> <hr> <p>From your comment below:</p> <blockquote> <p>I just need to create an async function inside which I call a normal synchronous function. When I mean asynchronous function I mean that when I call this function it doesn't obstruct the flow and the flow goes to the next function and this function can do something(like logging) after completion</p> </blockquote> <p>That's not quite the same thing your question asks. :-)</p> <p>First off, there are no asynchronous functions (in JavaScript). When you call a function in JavaScript, that function is executed synchronously. So that means you have two choices:</p> <ol> <li><p>The calling code can use <code>setImmediate</code> to tell Node to call the function asynchronously, or</p></li> <li><p>The function can do that for itself, probably using a nested function.</p></li> </ol> <p>Here's #1:</p> <pre><code>function foo() { console.log("A"); setImmediate(bar); console.log("B"); } function bar() { console.log("C"); } foo(); </code></pre> <p>which outputs:</p> <pre>A B C</pre> <p>Here's #2:</p> <pre><code>function foo() { console.log("A"); bar("C"); console.log("B"); } function bar(arg) { setImmediate(run); function run() { console.log(arg); } } foo(); </code></pre> <p>...which also outputs</p> <pre>A B C</pre> <p>Note how "C" was passed as an argument to <code>bar</code>. <code>bar</code> does nothing but schedule the callback to its nested function <code>run</code>. <code>run</code> uses the argument.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload