Technologies:
Tolerim
25 days ago
Reworded: Execute the if statement again after the else statement has executed.
createFunc
that will go back and forth between if
and else
statements until it is complete. I have shared the code below.
const mainFunc = (fn) => {
return async (query_name, ...args) => {
const result = await axiosFunc(queryname);
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) {
// Need to 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
initially checks the first if
statement (d.connector == null
). If true, it should run the axios_Func
. If the response has errors, I would like to rerun the function to handle them.
- I need guidance on how to rerun a function when errors occur. The code sometimes works when rerun manually.
if
statement is false, it then moves to the next else
statement. There, it checks if the name
is part of the dev
object previously declared in the createFunc
. If true, it runs the axiosDevFunc
. If false, it runs the axiosProdFunc
. Once this is complete, I would like to run the axios_DevFunc
again.
- Currently, it seems that the code is running out of order, and I'm not getting any errors. How can I get it to run as intended?
Answers(2)
Vogi
24 days ago
Verified Answer
To rerun a function when errors occur in JavaScript, you can use a try-catch block. If an exception is found, you can call the function again. Here's an example:
async function myFunction() {
try {
// some code that may throw an error
} catch (error) {
console.error(error);
await myFunction();
}
}
Regarding your second question, it seems that the code is running out of order because the axios_DevFunc is asynchronous and returns a promise. You can use await to wait for the promise to resolve before moving on to the next statement. Here's an example:
if (d.name in dev) {
await axios_DevFunc({ graphQL query }).then((response) => {
console.log(`created from dev`);
console.log(JSON.stringify(response.data, null, 4));
});
} else {
await 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));
});
});
});
I hope this helps! Let me know if you have any other questions.
Sources
Restart iteration in a for loop when an error is thrownAsync Await Error Handling in JavaScript - The Code BarbarianThe Ultimate Guide to JavaScript Error Handling — SitePointHow to continue javascript execution when an error occursnode.js - javascript catch errors on function call - Stack OverflowTolerim
25 days ago
To rerun a function in case of errors, you can use a recursive approach. You can create a new function that calls itself in case of errors until the function executes successfully. For example:
const runFunc = async (query, func) => {
try {
const response = await axios_Func(query);
if (response.data.errors) {
console.log('Error occurred, retrying...');
await runFunc(query, func);
return;
}
await func(response);
} catch (error) {
console.log(error);
}
};
In this code, we have defined a new function runFunc which takes a query and a callback function as parameters. It first calls the axios_Func with the given query and checks if the response has any errors. If there are errors, it logs a message and calls itself with the same parameters to retry the function. If there are no errors, it calls the given callback function with the response.
To run axios_DevFunc again after axios_ProdFunc, you can call it inside the .then block of axios_ProdFunc:
axios_ProdFunc({ graphQL query }).then((response) => {
console.log(JSON.stringify(response.data, null, 4));
return axios_DevFunc({ graphQL query });
}).then((response) => {
console.log(`created from dev`);
console.log(JSON.stringify(response.data, null, 4));
});
By returning axios_DevFunc from the axios_ProdFunc .then block, it will wait for axios_ProdFunc to finish before executing axios_DevFunc. Finally, the response from axios_DevFunc is logged to the console.
To ensure that the function runs in the intended order, you should organize the code into separate functions that return promises and chain them together using .then blocks. This will ensure that each function executes only after the previous one has finished.