简体   繁体   English

如何组织我的 javascript 代码而不是嵌套回调?

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

I'm making a application in javascript (Nodejs), I'm kinda new to it.我正在 javascript (Nodejs) 中申请,我对它有点陌生。 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当然,这对我来说不是 go 的正确方法......所以我发挥了创意并写了这个

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任何建议将不胜感激,谢谢

you can try this code.你可以试试这个代码。 if execute function throw an error, it will be caught by the try-catch block in the main function如果execute function报错,会被main function中的try-catch块捕获

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM