Sync version of async method

Have a look at CountDownLatch. You can emulate the desired synchronous behaviour with something like this: private CountDownLatch doneSignal = new CountDownLatch(1); void main() throws InterruptedException{ asyncDoSomething(); //wait until doneSignal.countDown() is called doneSignal.await(); } void onFinishDoSomething(){ //do something … //then signal the end of work doneSignal.countDown(); } You can also achieve the same behaviour using … Read more

Sending one AJAX request at a time from a loop

You want something like this. I haven’t tested it, but hopefully you should get the idea. var dates = []; //<– Contains a list of dates for the coming week var baseUrl = “http://www.someserver.com”; var storedChannels = [1,2,3,4,5,6,7,8,9,10,45,23,56,34,23,67,23,567,234,67,345,465,67,34]; function ProcessNext(ch, d) { if (d < 7) { d++; } else { d=0; if (ch < … Read more

Some clarification needed about synchronous versus asynchronous asio operations

The Boost.Asio documentation really does a fantastic job explaining the two concepts. As Ralf mentioned, Chris also has a great blog describing asynchronous concepts. The parking meter example explaining how timeouts work is particularly interesting, as is the bind illustrated example. First, consider a synchronous connect operation: The control flow is fairly straightforward here, your … Read more

Why are javascript promises asynchronous when calling only synchronous functions?

The callback passed to a Promise constructor is always called synchronously, but the callbacks passed into then are always called asynchronously (you could use setTimeout with a delay of 0 in a userland implementation to achieve that). Simplifying your example (and giving the anonymous function’s names so I can refer to them) to: Promise.resolve().then(function callbackA … Read more

What is the right way to make a synchronous MongoDB query in Node.js?

ES 6 (Node 8+) You can utilize async/await await operator pauses the execution of asynchronous function until the Promise is resolved and returns the value. This way your code will work in synchronous way: const query = MySchema.findOne({ name: /tester/gi }); const userData = await query.exec(); console.log(userData) Older Solution – June 2013 😉 Now the … Read more