Why is node.js asynchronous?

Node.js runs on a single thread whilst scripting languages use multiple threads. Not technically. Node.js uses several threads, but only one execution thread. The background threads are for dealing with IO to make all of the asynchronous goodness work. Dealing with threads efficiently is a royal pain, so the next best option is to run … Read more

Load and execute javascript code SYNCHRONOUSLY

If you use this: function loadScriptSync (src) { var s = document.createElement(‘script’); s.src = src; s.type = “text/javascript”; s.async = false; // <– this is important document.getElementsByTagName(‘head’)[0].appendChild(s); } You can do what you want (although divided up in an additional script file) test.html (code inside a script tag): loadScriptSync(“script.js”); loadScriptSync(“sayhi.js”); // you have to put … Read more

JavaScript: Global variables after Ajax requests [duplicate]

What you expect is the synchronous (blocking) type request. var it_works = false; jQuery.ajax({ type: “POST”, url: ‘some_file.php’, success: function (data) { it_works = true; }, async: false // <- this turns it into synchronous });​ // Execution is BLOCKED until request finishes. // it_works is available alert(it_works); Requests are asynchronous (non-blocking) by default which … Read more

How to force Sequential Javascript Execution?

Well, setTimeout, per its definition, will not hold up the thread. This is desirable, because if it did, it’d freeze the entire UI for the time it was waiting. if you really need to use setTimeout, then you should be using callback functions: function myfunction() { longfunctionfirst(shortfunctionsecond); } function longfunctionfirst(callback) { setTimeout(function() { alert(‘first function … Read more

document.createElement(“script”) synchronously

You can create your <script> element with an “onload” handler, and that will be called when the script has been loaded and evaluated by the browser. var script = document.createElement(‘script’); script.onload = function() { alert(“Script loaded and ready”); }; script.src = “http://whatever.com/the/script.js”; document.getElementsByTagName(‘head’)[0].appendChild(script); You can’t do it synchronously. edit — it’s been pointed out that, … Read more

Simplest way to wait some asynchronous tasks complete, in Javascript?

Use Promises. var mongoose = require(‘mongoose’); mongoose.connect(‘your MongoDB connection string’); var conn = mongoose.connection; var promises = [‘aaa’, ‘bbb’, ‘ccc’].map(function(name) { return new Promise(function(resolve, reject) { var collection = conn.collection(name); collection.drop(function(err) { if (err) { return reject(err); } console.log(‘dropped ‘ + name); resolve(); }); }); }); Promise.all(promises) .then(function() { console.log(‘all dropped)’); }) .catch(console.error); This drops … Read more

How to wrap async function calls into a sync function in Node.js or Javascript?

deasync turns async function into sync, implemented with a blocking mechanism by calling Node.js event loop at JavaScript layer. As a result, deasync only blocks subsequent code from running without blocking entire thread, nor incuring busy wait. With this module, here is the answer to the jsFiddle challenge: function AnticipatedSyncFunction(){ var ret; setTimeout(function(){ ret = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)