javascript - Async/Await - 如何在递归 Ajax 函数中实现 Javascript Async-Await?

标签 javascript ajax asynchronous async-await synchronous

我有两个功能。

我在 syncTrendyolOFFStocks() 函数中调用了多次 trendyolStocksUpdate() 函数。

我使用了 async/awaittrendyolStocksUpdate() 函数没有按顺序调用。它同时运行。

我的代码有什么问题?

下面是两个函数:

async function syncTrendyolOFFStocks(){

  //This function sends "out of stock products" one by one
  //to trendyolStocksUpdate function, so they their quantity will be initalized to 0
  //For example, T-Shirt XL Yellow, and T-Shirt XXL Red are sent.

  for(var product in all){
    var colors = all[product];
    for(var singleColor in colors[0]){
      var size = colors[0][singleColor];
      for(var index in size){
        var singleSize = size[index];
        await trendyolStocksUpdate(0,"",allProducts[product],singleSize,singleColor,0);
      }
    }
  }
}


async function trendyolStocksUpdate(stockCount,price,product,size,color,rowCount){

  //This function sends given Product Variation to trendyolUpdateStock.php with jQuery.ajax
  //recursively. 1000 product's quantity are equalized to 0, once at a time.

  dataObj = {
    rowCount: rowCount,
    product: product,
    size:size,
    color:color,
    stockCount: stockCount,
  };

  if(price != ""){
    dataObj["price"] = price;
  }

  await jQuery.ajax({
         type: "POST",
         url: "/wp-content/plugins/promc/templates/trendyolUpdateStock.php",
         dataType: 'json',
         data: dataObj,
         success: function(data)
         {

           if(data.numberOfRows == 1000)
           {
              rowCount = rowCount + 1000;
              trendyolStocksUpdate(stockCount,price,product,size,color,rowCount);
           }
           else
           {
             jQuery("#trendyolMonitor").append("<h3>Completed!</h3>");
             rowCount = 0;
           }
         },
         error: function(XMLHttpRequest, textStatus, errorThrown)
         {
         }
    });
}

最佳答案

当您在成功函数内调用 trendyolStocksUpdate(stockCount,price,product,size,color,rowCount); 时,它不会被等待。所以函数会在递归调用完成之前返回。

您可以在等待的 ajax 调用之后放置该逻辑,而不是使用成功函数。

async function trendyolStocksUpdate(stockCount,price,product,size,color,rowCount){
  //This function sends given Product Variation to trendyolUpdateStock.php with jQuery.ajax
  //recursively. 1000 product's quantity are equalized to 0, once at a time.

  dataObj = {
    rowCount: rowCount,
    product: product,
    size:size,
    color:color,
    stockCount: stockCount,
  };

  if(price != ""){
    dataObj["price"] = price;
  }

  try {
    const data = await jQuery.ajax({
      type: "POST",
      url: "/wp-content/plugins/promc/templates/trendyolUpdateStock.php",
      dataType: 'json',
      data: dataObj
    });
    if(data.numberOfRows == 1000)
    {
      rowCount = rowCount + 1000;
      await trendyolStocksUpdate(stockCount,price,product,size,color,rowCount);
    }
    else
    {
      jQuery("#trendyolMonitor").append("<h3>Completed!</h3>");
      rowCount = 0;
    }
  } catch (error) {
    // Error stuff…
  }
}

关于javascript - Async/Await - 如何在递归 Ajax 函数中实现 Javascript Async-Await?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59219048/

相关文章:

javascript - 刷新页面 Ajax Flask Web 应用程序

c# - 异步内存流处理方法 : which of the following?

javascript - 如何在 Bootstrap 4 中使 5 列相等?

php - 从动态创建的表中搜索数据

javascript - Ruby on Rails :Can not call ajax callback from form_tag

c# - 将任务标记为已完成

javascript - 如何让 JavaScript 回调等待另一个回调?

javascript - 切换最近的 div 位置

php - 如何在禁用浏览器的 javascript 时验证表单数据?

javascript - 使用UTF-8输出奇怪的字符