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

Can AFNetworking return data synchronously (inside a block)?

To block the execution of the main thread until the operation completes, you could do [operation waitUntilFinished] after it’s added to the operation queue. In this case, you wouldn’t need the return in the block; setting the __block variable would be enough. That said, I’d strongly discourage forcing asynchronous operations to synchronous methods. It’s tricky … 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

asynchronous and non-blocking calls? also between blocking and synchronous

In many circumstances they are different names for the same thing, but in some contexts they are quite different. So it depends. Terminology is not applied in a totally consistent way across the whole software industry. For example in the classic sockets API, a non-blocking socket is one that simply returns immediately with a special … Read more

Wait for Shell to finish, then format cells – synchronously execute a command

Try the WshShell object instead of the native Shell function. Dim wsh As Object Set wsh = VBA.CreateObject(“WScript.Shell”) Dim waitOnReturn As Boolean: waitOnReturn = True Dim windowStyle As Integer: windowStyle = 1 Dim errorCode As Long errorCode = wsh.Run(“notepad.exe”, windowStyle, waitOnReturn) If errorCode = 0 Then MsgBox “Done! No error to report.” Else MsgBox “Program … Read more

Are all javascript callbacks asynchronous? If not, how do I know which are?

I’m curious as to whether all javascript callbacks are asynchronous No. For instance, the callback used by Array#sort is not asynchronous, nor is the one used by String#replace. The only way you know whether a callback is asynchronous is from its documentation. Typically, ones involving requests for external resources (ajax calls, for instance) are asynchronous, … Read more