JSFiddle - React, Tailwind, and code Playground

by ACAPJS

HTML

<input id="token" name="token" placeholder="Enter API access token" />
<button id="buttonSave" name="buttonSave"> Simulate and Handle 429! </button>

JavaScript

$(document).on('click', '#buttonSave', function() {
  for (var i = 1; i <= 5; i++) {
    var token = 'oauth ' + $("#token").val();
    simulate(i, token);
  }

  function simulate(i, token) {
    var index = i;
    $.ajax({
      url: 'https://captivateprime.adobe.com/primeapi/v2/dummy',
      headers: {
        'Authorization': token
      },
      success: function() {
        console.log('success for call ' + index);
      },
      error: function(request, textStatus, errorThrown) {
        //handle 429 error
        if (request.status == 429) {
          console.log('429 detected. Retrying call ' + index + ' after ' + request.getResponseHeader('retry-after')  + 'ms');
          setTimeout(() => {
            console.log('retrying call ' + index);
            $.ajax(this);
          }, request.getResponseHeader('retry-after'));
        } else {
          //handle other errors
          console.log('other error encountered for call ' + index);
        }
      }
    });
  }
});