Make XMLHttpRequest synchronous when "async" option is true?

The signature of the open method of XMLHttpRequest looks like this:

void open(DOMString method, DOMString url, optional boolean async, optional DOMString? user, optional DOMString? password);

The third parameter of open is async . When set to false, the XMLHttpRequest object becomes synchronous, which means it will block the executing process until the request if finished.

I was wondering whether I can write a (wrapper) function getUrlSync(String url) if the async option is set to true . If so, this might be a general approach to convert an Asynchronous API into a Synchronous one.

I know forcing a async API to be sync is awful, but I was just curious whether it is possible..

I don't think it is simple to do that. To check whether the HTTP request is finished, a polling is need within a while loop. But as Javascript is in a single-thread environment, the intepreter will seem to be stuck in the while loop forever and never do the deferred HTTP request.. Does that make sense?

Does anyone have any ideas about this?

链接地址: http://www.djcxy.com/p/55342.html

上一篇: 取消链接调用,触发错误链

下一篇: 当“异步”选项为真时,使XMLHttpRequest同步?