小编Mat*_*ean的帖子

在异步函数中使用setTimeout

我有一个异步函数,在继续之前等待axios调用完成.问题是我需要将axios调用的超时时间设置为半秒,这样我才能达到shopify API调用限制.

async function processMatchingSchools(customer_metafield_url) {
  for (const item of customer_metafield_url) {
    await axios.get(item).then((res) => {
      for (key in res.data.metafields) {
        if (res.data.metafields[key].value === schoolName) {
          id_for_each_student.push(shopifyAdmin + "/customers/" + res.data.metafields[key].owner_id + "/metafields.json")
        }
      }
    })
  }
  console.log("Customer metafields to search", id_for_each_student)
  processOwnerIds(id_for_each_student)
}
Run Code Online (Sandbox Code Playgroud)

当我尝试设置setTimeout时,它会调用setTimeout并在完成axios调用之前继续.

async function processMatchingSchools(customer_metafield_url) {
  for (const item of customer_metafield_url) {
    await setTimeout(function(item) {
      axios.get(item).then((res) => {
        for (key in res.data.metafields) {
          if (res.data.metafields[key].value === schoolName) {
            id_for_each_student.push(shopifyAdmin + "/customers/" + res.data.metafields[key].owner_id + "/metafields.json")
          }
        } …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous settimeout async-await axios

2
推荐指数
4
解决办法
4228
查看次数