0

I'm making a application in javascript (Nodejs), I'm kinda new to it. My code needs to do multiple congruent requests, I organized my code in async functions so I can linearly call them

my first code looked like this

async function Fa(param,param1,callback,error){
//SOME CODE
}

async function Fb(param){
//SOME CODE
}

async function Fc(param){
//SOME CODE
}

function Fd(param,callback,error){
//SOME CODE
}

and use it like this

Fa(param,param1,
    (result,result1) => {
        Fb(resultB) => {
           Fc(resultB);
        }
     },
     (error) => { /*handle error*/ }
);

Fd(param,
  (result)=>{
    //handle result
  },
  (error)=>{
    //handle error
  }
)

of course this is not the right way to go for me... so I got creative and wrote this

async function Fa(param,param1){
  var errorFun,resultFun;
  function setOnError(error){errorFun = error;}
  function setOnResult(result){resultFun = result;}

  async function execute(){
    //SOME CODE HERE
  }
  return {setOnError,setOneResult,execute} 
  //I had to write a execute function because `Fa` being an async function I couldn't access setError and other inner functions from outside 
}

I'm not repeating all the functions but I hope you got the idea

so my code looks like this

var resultA,resultA1; 
var fa = await Fa(param,param1);
fa.setOnError((error) => /*handle error*/ );
//I want to terminate my code here (all this being in a function) but I don't know how to do so because I can't even set a flag to understand if error function has been called because I have multiple function with error and setting multiple flags would be stupid 
fa.setOnResult( (result,result1) => {resultA = result; resultA1 = result1} );
await fa.execute()

var fb = await Fb(param);
fb.setOnResult((result) => {Fc(result);})
await fb.execute();

var fd = await Fd(param);
fd.setOnResult(/*some code*/);
fd.setOnError(/*some code*/);
await fd.execute();

I like my second version more but I don't know how to handle the errror (I want to stop executing the main function) and I think it's a bit overkill..

Any suggestion will be appreciated, thank you

Mirco0
  • 306
  • 1
  • 2
  • 8
  • 4
    Do you really *need* a function to both take a callback and be async at the same time? Can you not just use promises and thus chain them? [Aren't promises just callbacks?](https://stackoverflow.com/q/22539815) – VLAZ Jan 23 '23 at 15:04
  • @VLAZ thank you, I'll check it and try to chain promises – Mirco0 Jan 23 '23 at 15:08
  • 1
    It looks like you are trying to reinvent promises. But we can't really tell what you need unless you post your actual code with the `//SOME CODE` implementations. Please [edit] your question to include them. – Bergi Jan 23 '23 at 15:23
  • @Bergi //SOME CODE is just the implementation of my code, I guess you want to know if I call the error and success function inside the function... And I'm calling them inside my functions, so probably, I'm just reinventing promises because of my inexperience in javascript – Mirco0 Jan 23 '23 at 15:32
  • @Mirco0 Yes, but where and how often? What does the rest of the code do, does it use `await`? Why do you not use `return` and `throw`? Please just post your code so we can see. – Bergi Jan 23 '23 at 15:38
  • oh my bad, yes it uses await, the error function is called in every unexpected result (once or twice) and the result function gets called only once, the function is returned after one of the two is called – Mirco0 Jan 23 '23 at 15:41
  • Twice, like, in a row? Why? And what do you mean by "*the function is returned*"? Again, please post your code, we cannot answer your question without it. – Bergi Jan 23 '23 at 16:13
  • @Bergi NOT TWICE IN A ROW but like `if a == undefined` error(someError)` or `if a > 3` error(someError)` after calling error or result I also call `return` That's it. – Mirco0 Jan 23 '23 at 16:18
  • But you `return` nothing? Don't do that. Just throw an error instead of calling the error callback, just return the result value instead of calling the result callback! – Bergi Jan 23 '23 at 16:25
  • @Bergi thank you for explaining, I'm now trying to use Promises, I will edit my question if something goes wrong. – Mirco0 Jan 23 '23 at 22:20

1 Answers1

0

you can try this code. if execute function throw an error, it will be caught by the try-catch block in the main function

async function Fa(param, param1) {
  var errorFun, resultFun;
  function setOnError(error) { errorFun = error; }
  function setOnResult(result) { resultFun = result; }

  async function execute() {
    //SOME CODE HERE
    if (error) {
      throw new Error(error);
    }
  }
  return { setOnError, setOnResult, execute }
}

async function main() {
  try {
    var fa = await Fa(param, param1);
    fa.setOnError((error) => /*handle error*/ );
    fa.setOnResult((result, result1) => { resultA = result; resultA1 = result1 });
    await fa.execute();

    var fb = await Fb(param);
    fb.setOnResult((result) => { Fc(result); });
    await fb.execute();

    var fd = await Fd(param);
    fd.setOnResult(/*some code*/);
    fd.setOnError(/*some code*/);
    await fd.execute();
  } catch (error) {
    // handle error
  }
}
Mandeep
  • 364
  • 2
  • 16
  • I'm now trying Promises but I really like it, maybe it's the missing piece I was searching for. Thank you – Mirco0 Jan 23 '23 at 15:30