在异步函数中使用setTimeout

时间:2018-07-05 22:21:29

标签: javascript asynchronous async-await settimeout axios

我有一个异步功能,等待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)
}

当我尝试放置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")
          }
        }
      })
    }, 500)
  }
  console.log("Customer metafields to search", id_for_each_student)
  processOwnerIds(id_for_each_student)
}

有帮助吗?

5 个答案:

答案 0 :(得分:5)

await仅适用于诺言。 您需要将setTimeout包装在一个Promise中:

const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));

await waitFor(500);

答案 1 :(得分:3)

setTimeout()不会返回Promise,但是您可以像这样将其包装。我还清理了其余的代码。

async function processMatchingSchools(customer_metafield_url) {
  for (const item of customer_metafield_url) {
    await new Promise(resolve => {
      setTimeout(resolve, 500)
    })

    await axios.get(item).then((res) => {
      Object.values(res.data.metafields).filter(
        ({ value }) => value === schoolName
      ).forEach(({ owner_id }) => {
        id_for_each_student.push(`${shopifyAdmin}/customers/${owner_id}/metafields.json`)
      })
    })
  }

  console.log("Customer metafields to search", id_for_each_student)

  processOwnerIds(id_for_each_student)
}

答案 2 :(得分:1)

创建一个sleep函数,该函数返回可以使用的promise,例如:

const sleep = (milliseconds=500) => new Promise(resolve => setTimeout(resolve, milliseconds))

并在异步功能中使用它:

(async () => {
  console.log("function invoked...")
  await sleep(500)
  console.log("I got here about 500 milliseconds later")
})()

答案 3 :(得分:1)

setTimeout不返回承诺,因此无法Site Species Season Conductance 1 Species1 Dry 20 1 Species2 Dry 30 1 Species3 Dry 40 1 Species1 Dry 25 1 Species2 Dry 30 1 Species3 Dry 35 2 Species1 Dry 30 2 Species2 Dry 24 2 Species2 Dry 25 2 Species4 Dry 30 2 Species4 Dry 32 2 Species4 Dry 30 1 Species1 wet 26 1 Species2 wet 24 1 Species3 wet 30 2 Species1 wet 32 2 Species4 wet 30 2 Species4 wet 28 编辑。

您可以创建自己的基于承诺的await并使用它。

setTimeout

答案 4 :(得分:0)

例如,您需要创建新的承诺

function delay(ms){
 return new Promise(resolve => setTimeout(resolve, ms))
}

然后在调用API之前在代码中使用它

...
await delay(500)
await axios.get(item).then((res) => {
...