I’d like to await a function, call it
runThirdPartyCall()
however, I don’t know how long this will take ahead of time. If it takes longer than 30 seconds, i’d like to return from the request early, but still be able to reason about what runThirdPartyCall()
returns.
Is this possible to do with workers? I see that javascript cannot directly hand over the execution of a function or the promise it returns to another thread or process once it has started, so i’m wondering if there are any alternatives.
I’m stuck wondering about the general problem, when you have a third party call of which you don’t know how long it will take, and if it ends up taking too long you want to go and inform the client/move on to other things
2
Answers
Use the message facility to communicate the answer back to your mainline from the worker: –
Mainline
I think your requirements need to be clearer, but anyway: –
This code does NOT start on the UI thread and then miraculously switch to a WORKER thread but I believe it could match your requirements.
Mainline: –
demo_worker.js