Currently, I am delving into the node.js async module and wondering if it's possible to modify the behavior of the async.retry method. Specifically, I'd like it to retry even on successful operations but halt based on a certain condition or response - for instance, in the case of an API call.
Based on the documentation, this function will keep attempting the task upon failures until it succeeds. Once it does succeed, it will only run that one time. But how can I achieve the same behavior on successful operations and have it stop under a specific condition?
const async = require('async');
const axios = require('axios');
const api = async () => {
const uri = 'https://jsonplaceholder.typicode.com/todos/1';
try {
const results = await axios.get(uri);
return results.data;
} catch (error) {
throw error;
}
};
const retryPolicy = async (apiMethod) => {
async.retry({ times: 3, interval: 200 }, apiMethod, function (err, result) {
// should retry until the condition is met
if (result.data.userId == 5) {
// stop retrying
}
});
};
retryPolicy(api);