To not hang the browser, you really should return from your plugin function immediately and non-blockingly do the work in background thread. When it is completed, you can invoke a callback to notify the content JavaScript.
Every workaround like spinning message loops is just that - a workaround - and not guaranteed to work across browser-versions. The only proper solution is to do the work non-blockingly.
You should always be able to handle your concerns without blocking the whole browser, like not processing any further actions on your web page while waiting for completion of the operation.