javascript 在运行else之后返回并运行if语句

twh00eeo  于 2023-05-05  发布在  Java
关注(0)|答案(1)|浏览(203)

我有一个函数需要在ifelse之间来回运行,直到完成。
我的代码:

const mainFunc = (fn) => {
  return async (query_name, ...args) => {
    const result = await axios_Func(query_name);
    try {
      return fn(result, ...args);
    } catch (e) {
      console.log(e);
    }
  };
};

const createFunc = mainFunc((result, table_id) => {
  (async () => {
    let dev = await getTabs({ query: tables(123) });
    let prod = await getTabs({ query: tables(321) });
    result.data.forEach((d) => {
      if (d.connector == null) {
        axios_Func({ graphQL query }).then((response) => {
          if (response.data.errors) {
            // rerun function on errors
          } else console.log(JSON.stringify(response.data, null, 4));
        });
      } else {
        if (d.name in dev) {
          axios_DevFunc({ graphQL query }).then((response) => {
            console.log(`created from dev`);
            console.log(JSON.stringify(response.data, null, 4));
          });
        } else {
          axios_ProdFunc({ graphQL query }).then((response) => {
            .then((res) => res)
            .then((x) => {
              //repeat Dev Function after axios_ProdFunc is finished
              axios_DevFunc({ graphQL query }).then((response) => {
                console.log(`created from dev`);
                console.log(JSON.stringify(response.data, null, 4));
              });
            });
        }
      }
    });
  })();
});

我的createFunc从第一个if (d.connector == null)开始。如果为真,则运行axios_Func。如果它返回错误,那么我想重新运行错误的函数。
1.我不确定如何在错误上重新运行函数。这些错误通常会突然出现,但当手动重新运行时,它们可以工作。
在进入else之后,我在里面有一个if,以检查name是否在函数前面声明的dev对象中。如果为真,则运行axios_DevFunc函数。否则先运行axios_ProdFunc
1.完成后,我希望能够再次运行axios_DevFunc
问题是,我的代码没有给我任何错误,但没有按顺序运行。一切似乎都是随机运行的。我如何让它以我上面提到的方式运行?

dw1jzc5e

dw1jzc5e1#

试试这个!

const mainFunc = (fn) => {
  return async (query_name, ...args) => {
    const result = await axios_Func(query_name);
    try {
      const res = await fn(result, ...args);
      return res
    } catch (e) {
      console.log(e);
    }
  };
};

const createFunc = mainFunc(async(result, table_id) => {
    let dev = await getTabs({ query: tables(123) });
    let prod = await getTabs({ query: tables(321) });
    result.data.forEach((d) => {
      if (d.connector == null) {
        axios_Func({ graphQL query }).then((response) => {
          if (response.data.errors) {
            // rerun function on errors
          } else console.log(JSON.stringify(response.data, null, 4));
        });
      } else {
        if (d.name in dev) {
          axios_DevFunc({ graphQL query }).then((response) => {
            console.log(`created from dev`);
            console.log(JSON.stringify(response.data, null, 4));
          });
        } else {
          axios_ProdFunc({ graphQL query }).then((response) => {
            .then((res) => res)
            .then((x) => {
              //repeat Dev Function after axios_ProdFunc is finished
              axios_DevFunc({ graphQL query }).then((response) => {
                console.log(`created from dev`);
                console.log(JSON.stringify(response.data, null, 4));
              });
            });
        }
      }
    });
});

相关问题