Reputation: 47101
Let's say there is a function like this in a third-party javascript (not NodeJS) module:
Api.IoAsync(parameter, function(err, message) {
...
})
And I want to convert it into a synchronous version like this:
Api.IoSync(parameter)
It will be blocking until complete, and its return value is message
if succeed, else err
.
Is there an universal and easy way to do this?
Upvotes: 0
Views: 155
Reputation: 708186
You cannot make an asynchronous function behave synchronously in Javascript. This has been asked many times. It cannot be done in Javascript.
If the underlying function is asynchronous, then you will have to code an asynchronous response callback and use the results of the async function that way. There is no work-around.
Learn how to code with async callbacks and move forward. Promises can simplify the use of asynchronous functions by allowing you to sequence and chain async operations more easily, but they still use callbacks to process results.
Upvotes: 1