How do I organize my javascript code instead of nesting callbacks?

92 Views Asked by At

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

1

There are 1 best solutions below

1
Mandeep On

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
  }
}