I've searched SO and the web for a while now, but nothing quite what I'm looking for has come up.
I'd like to wrap a function so that the function would run in the background and not in the main thread of a UI like React. From what I understand async/await
is does not quite do what I want here. I've read that Webworkers could be the way to go, and also that Internet Explorer 9 and older IE browsers do not support these Workers. That will probably not be an issue, especially moving forward.
What I'm looking for would ideally look something like this:
function someCumbersomeTask(args) {
/* This function takes some arguments and performs
* a heavy/long task which blocks the browser by default. */
return someReturnValue;
}
function runInBackground(functionToWrap, argsToFunction) {
/* This function would take another function (and possibly its
* parameters separately is that is the best way to handle this)
* and execute the function without blocking or, better even,
* touching the main JavaScript thread. The return value would
* be the same as the 'functionToWrap' would return. */
return someReturnValue
}
Ideally you would get the same value by calling the original function and "backgrounded" function:
const val1 = someCumberSomeTask(someArgs); // blocking
const val2 = runInBackground(someCumbersomeTask, someArgs); // non-blocking
console.log(val1 === val2); // -> true
The question is: is this possible with current JS? The functions do not have to look exactly like in the given examples, but the idea is that one would just have to wrap a function in something to make its execution totally non-blocking.