I'm porting a pile of C++ code to Javascript using the Emscripten system. The C++ code has many calls to fopen
which is a synchronous IO call. Within Emscripten, we simulate this using an XHR request to a local resource however , within Firefox synchronous XHR calls (with a responseType
of blob
or arraybuffer
) are only supported within a Web-Worker. Converting all that c++ code to adapt to asynchronous IO code seems very complicated so for my first try, I'd like to see if I can fake a synchronous XHR request.
My initial thought was that the main loop could share some state with a web-worker which could make the synchronous io call and update the shared state while the main loop paused and waited for the web-worker finished. DISCLAIMER: I know this is not the typical Javascript way but I am porting synchronous code, not writing new code from scratch (in which I would definitely have used asynchronous IO).
Given the restrictions on sharing state between a web-worker and the main-loop, this idea looks untenable.
Are there other ways to do this?
So after seeing all the answers and doing some of my own reading, it appears the best answer is: "You can, but only for text data and you have to convert it back to binary data". This is slow, but does work.
Have you looked at testing libraries like qunit and sinon? I think Jasmine may also be able to do it, but I know that sinon can do this
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.