1

I have a function that needs to keep running back and forth between the if and else till it is complete.

My code:

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));
              });
            });
        }
      }
    });
  })();
});

My createFunc starts with going through the first if (d.connector == null). If that is true then run the axios_Func. If it returns errors then I would like to rerun the function of the errors.

  1. I am unsure how to rerun a function on the errors. THese errors usually crop up sometimes but when manually rerun, they work.

After this going into the else, I have an if inside that to check if name is in the dev object declared earlier in the function. If that is true then run the axios_DevFunc function. Else run the axios_ProdFunc first.

  1. After that is complete, I want to be able to run the axios_DevFunc again.

The issue is, my code is not giving me any errors but is not running in order. Everything just seems to be running at once haphazardly. How do I get it to run in the way I have mentioned above?

nb_nb_nb
  • 1,243
  • 11
  • 36
  • it may be due to the awaits in an async function. – Amodh May 03 '23 at 17:40
  • I agree with @Amodh It seems like the issue is that you decided to make an async IIFE which is never awaited (not sure why you did that either). I'd recommend removing the IIFE and just making the arrow function on the line above it into an async arrow function. – Andria May 03 '23 at 18:21
  • @Andria I am unsure what you mean. What does IIFE mean? – nb_nb_nb May 03 '23 at 18:22
  • @Amodh I am unsure what you mean – nb_nb_nb May 03 '23 at 18:23
  • @nb_nb_nb an IIFE is an Immediately Invoked Function Expression. It's your `(async () => {})()`. You're creating a function and then executing it. You also never `await`ed it so none of your code is going to wait until that IIFE is done. – Andria May 03 '23 at 18:24
  • @nb_nb_nb Are you perhaps looking for the ability to retry an Axios request? If so this [post on how to retry an Axios request with `axios-retry`] would be helpful. If not, are you just trying to infinitely retry? Because you should be able to use `Infinity` with `axios-retry` for that. – Andria May 03 '23 at 18:43
  • @Andria, I don't think you linked the post. – nb_nb_nb May 03 '23 at 19:07
  • 1
    @nb_nb_nb My aplogies here it is: [post on how to retry an Axios request with `axios-retry`](https://stackoverflow.com/questions/56074531/how-to-retry-5xx-requests-using-axios) – Andria May 04 '23 at 16:00

1 Answers1

-2

Try this!

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));
              });
            });
        }
      }
    });
});
Amodh
  • 367
  • 1
  • 3
  • 16