I have chained requests on GA API, meaning the second requests uses result from the first request. I wanted to use exponentialBackoff()
function to retry if there is an error (status 429 for example).
My question is, what would be the best way of checking that both of the requests were successful and only repeat the one which wasn't. In my example, both of the requests get called again even if only the second one is not successful.
async function exponentialBackoff(toTry, max, delay) {
console.log('max', max, 'next delay', delay);
try {
let response = await toTry()
console.log(response);
} catch (e) {
if (max > 0) {
setTimeout(function () {
exponentialBackoff(toTry, --max, delay * 2);
}, delay);
} else {
console.log('we give up');
}
}
}
propertyArr.forEach(async function (e, i, a) {
await exponentialBackoff(async function () {
let response = await gapi.client.request({
path: 'https://analyticsadmin.googleapis.com/v1beta/properties',
method: 'POST',
body: e
})
return await gapi.client.request({
path: 'https://analyticsadmin.googleapis.com/v1beta/' + response.result.name + '/dataStreams',
body: dataStreamArr[i],
method: 'POST'
})
}, 10, 30)
})