Im sorry i know that there are many questions regards this question but i couldn't apply it's answer on my code, maybe because im still new to js.
Here is my code, that code simply supposed to loop on product_id array but when i fire that function i see thatajax requests for all array elements got sent at once, what im trying to do is to start next loop after finishing the current loop.
function apm_update_all_products_ajax() {
  $product_ids = wc_get_products( array( 'return' => 'ids', 'limit' => -1 ) );
  ?>
  <button onclick="apm_update_all_products()">Update All Products</button>
      <script type="text/javascript" >
          function apm_update_all_products() {
          var product_ids = <?php echo json_encode($product_ids); ?>;
          for (var product_id of product_ids) {
                        $.ajax({
                            type: "POST",
                            url: ajaxurl,
                            dataType: "JSON",
                            data: {
                                    action: 'apm_update_single_product',
                                    postId: product_id
                            },
                            success: function(lookup_data) {
                            },
                            error: function(jqXHR, textStatus, errorThrown) {
                            }
                        })
          }
          };
    </script>
  <?php
  }
The reason i wanna do that is that this ajax send request to Amazon API which has a throtling limit so i cant send all requests at one time.
