1

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.

Aarni Joensuu
  • 711
  • 8
  • 19
  • 1
    Why did you stop researching about Web Workers? Sounds like you're almost there. And for IE there are polyfills that will emulate the WebWorker's API, even though it will still work on the main thread, at least you have a same code-base and only utterly dangerous browsers would suffer from that blocking function. – Kaiido Apr 16 '21 at 07:10
  • 1
    _"and also that Internet Explorer and older IE browsers do not support these Workers"_ - IE in itself isn't supported anymore. – Andreas Apr 16 '21 at 07:10
  • "Running in the background" is exactly what Web Workers were made for. You can achieve similar behavior using generators or promises, but both will block the main thread. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function* – mario_sunny Apr 16 '21 at 07:12
  • Possibly related: https://stackoverflow.com/questions/26615966/how-to-make-non-blocking-javascript-code – glinda93 Apr 16 '21 at 07:13
  • Are you trying to run some code in the background because you expect it to be computationally heavy? If that's the case, couldn't you just offload the computation to a server somewhere? – mario_sunny Apr 16 '21 at 07:16
  • I researched Workers quite ok IMO, just came across nothing where the end result would be simple. @mario_sunny You are correct that a server should handle heavy tasks. However, this is partially an out-of-interest question and my case is just that I'd like the browser to be responsive even the 0.5 sec time period which it otherwise would not be. And as the optimizer that I am, I'd like to do it "the rigth way" from the start :) – Aarni Joensuu Apr 16 '21 at 07:23
  • @AarniJoensuu Within the browser environment, there is no way to execute code in a separate thread without using Web Workers. As I said, Javascript provides ways of _emulating_ this behavior, but it's all running on a single thread from the programmer's perspective. – mario_sunny Apr 16 '21 at 07:27
  • @mario_sunny In that case I am looking for a functionality that utilizes Web Workers. – Aarni Joensuu Apr 16 '21 at 07:33

0 Answers0