1

For better structure of my script I want to eliminate .then() blocks. I.E. turn

callAsyncFunction().then( (result) => {
    console.log("done");

    callAnotherAsyncFunction().then( (result2) => {
        console.log("also done");

        // further steps ...
    });
});

into

callAsyncFunction().wait();
console.log("done");
callAnotherAsyncfunction().wait();
console.log("also done");
// further steps ...

I know JavaScript does not support blocking functions like wait() for a reason. But for my purpose it would be just perfect if there was a compiler/transcriptor that turns the code from the 2. example into the code of the 1st example. So it would not actually block a call but allow a better representation for callbacks. Is there a script language like dart maybe, that compiles into JavaScript, supporting such a feature? I could write my own compiler, but that would be a project in its own and take a while.

Also I am well aware of the async/await concept. Unfortunately I would have to declare all my functions up to the main() function as async in order to have it "block" at an await call.

I noticed a lot of people having that problem but there is no satisfying solution so far.

  • 1
    "*Unfortunately I would have to declare all my functions up to the main() function as async in order to have it "block" at an await call.*" - that's exactly what you *should* do if you want your entire script to execute sequentially. And also it's exactly what a transpiler would use to run your script with the weird `.wait()` calls. Why not just write the standard `await`? Especially if you already *have* promise-based code with `then` calls, it's the obvious and proper solution. – Bergi Oct 25 '20 at 23:34
  • I was wondering if somebody would suggest that as a valid solution. Ok, I'll try to validate that for me. There are reasons that speak against it like Performace, using of Promise.all() is still ugly, bad feeling in the stomach to call a function async that actually isn't async, ... – Colonel Panic Oct 25 '20 at 23:46
  • Maybe I should mention that it is a game and all of its functions would have to be async then. Can that be good practice? But well, ... I"ll validate that for me. – Colonel Panic Oct 25 '20 at 23:50
  • What do you mean by "*that actually isn't async*"? Surely if it calls other asynchronous functions, and wants to wait for their results, that **does** make it asynchronous. – Bergi Oct 25 '20 at 23:50
  • And no, [not *all* of your functions should become `async`](https://stackoverflow.com/a/35380604/1048572). Only those that currently are asynchronous already, i.e. using promises and calling `then`. – Bergi Oct 25 '20 at 23:51

1 Answers1

0

You can use async/await for that. It allows you to write almost sequential code instead of dealing with .then. Remember that you cannot use await on top level, it always needs to be inside of an async function, for example like here:

const run = async () => {
  const result = await callAsyncFunction()
  console.log("done");
  const result2 = await callAnotherAsyncFunction();
  console.log("also done");
  // further steps ...
}

run();
IvanD
  • 2,728
  • 14
  • 26