0

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)
        })
  • In case of 429 I wouldn't use exponential backoff but rather the recommended delay from the response headers – Bergi Aug 12 '22 at 12:50
  • a) make sure `exponentialBackoff` returns a promise for the eventual result b) call it twice, on every individual `gapi.client.request` c) consider using a sequential loop over `propertyArr` instead of [firing all requests at once](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop?rq=1) - backoff won't help you there if every `propertyArr` element does its own retry – Bergi Aug 12 '22 at 12:52
  • @Bergi can you please guide me on how to do c)? Do you mean I should wait for each request to fullfil before calling another one? thank you! – iCanLevitate Aug 12 '22 at 14:31
  • @Bergi Ive updated the code usync async/await, please check it out. However There is still the same issue with the first request repeating as well even when only the second one fails leading to duplicating the first request. – iCanLevitate Aug 12 '22 at 14:38
  • You'll need to use `const response = await exponentialBackoff(() => gapi.client.request(…)); exponentialBackoff(() => gapi.client.request(…response…));`. And your `exponentialBackoff` implementation still doesn't properly return the eventual result in all cases – Bergi Aug 12 '22 at 23:56
  • what do you mean, it wouldn't return the eventual results in all cases? thank you btw, your code works @Bergi – iCanLevitate Aug 14 '22 at 10:35
  • Well the `exponentialBackoff` you posted doesn't `return` anything, even less so in the "recursive" case – Bergi Aug 14 '22 at 22:21
  • ahh, yea, I already modified this and returned the response in the function, thank you – iCanLevitate Aug 15 '22 at 07:47

0 Answers0