问题描述
JavaScript使用在大多数现代的API处理像磁盘IO和网络的慢的事情异步调用。我意识到发生了什么这样做的目的,但有使真正需要同步调用时,某些情况下。
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 cases when making synchronous calls really needed.
例如,我有一个JavaScript code,我不能重写。在code有一定的方法同步调用。对于一些调试目的在开发环境我想截获此调用,将信息发送到服务器,并等待服务器响应。这个拦截器将不会对生产工作。据我所知,有办法用NPAPI做到这一点,但现在是pcated正式去$ P $。而无论谷歌Chrome和Mozilla Firefox浏览器只提供异步socket API,所以我没有机会进行这样的拦截。另一种方式是发送syncrhonous XMLHtt prequest,但大大慢,在IS需要HTTP连接和HTTP有效载荷,所以我会preFER WebSocket的。
For example, I have a JavaScript code that I can't rewrite. In the code there are synchronous call to a certain method. For some debugging purposes at developer's environment I want to intercept this call, send information to server and wait until server responds. This interceptor won't work on production. AFAIK, there was a way to do this with NPAPI, but now it is officially deprecated. And both Google Chrome and Mozilla Firefox provide only asynchronous socket API, so I have no chance to perform such interception. Another way is to send syncrhonous XMLHttpRequest, but is is considerably slow, at is requires HTTP connection and HTTP payload, so I would prefer WebSocket.
我有另一个角落的情况下,我要叫IndexedDB的,但它是很难解释。不幸的是IndexedDB的同步API是不是在任何地方实施。
I have another corner case where I have to call IndexedDB, but it is very hard to explain. Unfortunately IndexedDB sync API is not implemented anywhere.
是否有其他黑客同步调用异步方法?我会接受特定浏览器黑客,浏览器插件,插件等等。或者可能是有人知道如何要求所有那些开发网络标准委员会(和浏览器开发商也一样),包括syncrhonous API至少WebWorker版本?
Are there another hacks to call asynchronous methods synchronously? I would accept browser-specific hacks, browser add-ons, plugins and so forth. Or may be someone knows how to ask all those committees (and browser developers as well) that develop Web standards to include at least WebWorker versions of syncrhonous API?
我知道,有关调用异步方法同步进行了多次询问的问题,以及共同的回答是:这是一个糟糕的主意。我知道的一般这是一个坏主意,但在某些:我们真的需要自由的情况下同步调用。
I know, that questions about calling asynchronous method synchronously were asked many times, and the common answer is "this is a bad idea". I realize that generally it is a bad idea, but in a certain cases we really need freedom to call synchronously.
推荐答案
我不知道任何方式。我通常描述异步code作为病毒:它很快会感染周围的code。与更多的异步性。我不知道有什么办法突破这一点,这似乎不太可能,我认为有一种方法。显然,有技术,让您的异步code看起来更加同步,与承诺开始,并持续到宏和等。但最终,运行时仍然是异步的。
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.
这篇关于黑客进行同步的JavaScript调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!