0

I know blocking the event loop is bad and the consequences of that. But even the native fs module provides some sync functions for some purposes, for example, CLIs using fs.readFileSync.

I'd like to convert the following async-await code to blocking code.

let value = undefined;

function setup() {
  return new Promise((resolve) => {
    setTimeout(() => {
      value = "**value**";
      resolve();
    });
  });
}

// need to convert below function to blocking
async function getValue() {
  await setup();
  return value;
}

console.log(await getValue()); // top level await OK

Assuming it is blocking, we can then call it like

getValue(); // without await or .then

I tried it like this. But it is not working.

function getValue() {
  setup();
  while (!value) {
    // wait until we get value
  }

  return value;
}

console.log(getValue())

How can I achieve this?

(The fs.readFileSync is blocking. Is there any way we can use the technique used by readFileSync)

Edit

Related thread: Convert asynchronous/callback method to blocking/synchronous method

Wajahath
  • 2,827
  • 2
  • 28
  • 37
  • 3
    No, that is not possible, as a "busy loop" prevents any event on the event queue to get executed, and that includes the triggering of a `setTimeout` callback. That callback -- which sets `value` -- can never get executed. You will serve your purposes best when you fully *embrace* the asynchronous coding pattern. Everything is possible with it. – trincot Apr 02 '22 at 08:29
  • You could wrap all your code in a callback passed to setup, and quickly fall into callback hell i guess. Or use the async await + top level await just as you did in your first example, could you explain why this is not satisfying ? – NyuB Apr 02 '22 at 08:33
  • 2
    *"The `fs.readFileSync` is blocking. Is there any way we can use the technique used by readFileSync"* No. `readFilesync` is synchronous because it actually does its work synchronously, instead of asynchronously like `readFile` does. You can't take an asynchronous process and make it synchronous in JavaScript code (in the general case). You have to instead make a new, synchronous process, which may or may not be possible depending on the environment and the process you're doing. For instance, in an extreme case, you could spawn a separate process to do the work... – T.J. Crowder Apr 02 '22 at 08:34
  • ...and wait for it synchronously via `execSync` etc. But... :-) – T.J. Crowder Apr 02 '22 at 08:35
  • 1
    I know the "XY problem" gets raised a lot in comments on here but this seems like [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Andy Apr 02 '22 at 08:38
  • OK. I could delete this question because some people found it is not useful. But I'm keeping it here despite the fact that it can receive further downvotes in future. Because this this knowledge will be useful for many. – Wajahath Apr 02 '22 at 09:07

1 Answers1

2

How can I achieve this?

You change language.

Seriously: JavaScript is (more or less, there are many caveats there) single threaded. Your callback code should be processed by the same thread you intend to block.

There is NO way you can achieve this: it's impossible.

So, either you change your language (in C# or Java you can spin up a thread and block it as you wish, while other threads do some work), or you change requirements.

I suspect this last option is the real solution to your alleged problem. Now, what is that you really need to do?

Alberto Chiesa
  • 7,022
  • 2
  • 26
  • 53