The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

ptmy (22) [Avatar] Offline
After reading much of the book, one thing that's still a bit fuzzy to me is how to go about creating an async API for an inherently synchronous task. Two hypothetical examples follow:

* I have to read data from a DB where DB creator only provided a synchronous API (e.g., syncRead). Is there anyway to wrap syncRead in Javascript to create an async version of this API for users to consume?

* I need to call a black-box function that performs some time consuming calculations. Since I don't have access to the internals of this function, I can't use the timer tricks mentioned in chapter 13 of the book to give the event-loop thread execution time. In traditional languages such as C++ or Java, I would just spawn a new thread to call the calculator. What's the best way of handling this in Javascript?

The ES6 "Promise" feature would be perfect for something like this if the Javascript engine uses a separate thread for executing the passed-in function to the "Promise" Constructor. But I'm under the impression from the book that this function is also executed by the event-loop thread.

For example:

myPromise = new Promise ((resolve, reject) => {
        let success = syncTimeConsumingFunction();
           if (success)