Skip to content Skip to sidebar Skip to footer

Hacks To Make Synchronous Javascript Calls

JavaScript uses asynchronous calls in most of the modern APIs dealing with 'slow' things like disk IO and network. I realize what the purpose of this, however there are certain cas

Solution 1:

I don't know of any way. I usually describe asynchronous code as viral: it quickly infects the code around it with more asynchronicity. I don't know any way to break that, and it seems unlikely to me that there is a way. Obviously there are techniques to make your asynchronous code look more synchronous, starting with Promises, and continuing into macros and such. But in the end, the runtime is still asynchronous.

Maybe someone smarter than me can provide an interesting suggestion. I'd love to hear it too, although I don't have any personal need at the moment.

Solution 2:

Some other ideas that you might want to consider:

For the web based traffic

Use a proxy server to intercept the communication. I found an article here that discusses using a proxy server with web sockets. A properly written proxy could hold onto the transferred data until its exchange with the server was complete, then pass things forward.

For the IndexedDb

You could create a wrapper function around the IndexedDb calls, or at least the ones you needed, that would allow you to interject whatever behavior you needed in. This wrapper could modify data and perform its own async business before invoking the callback method.

Post a Comment for "Hacks To Make Synchronous Javascript Calls"