相关疑难解决方法(0)

在异步函数中使用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
查看次数

每 2 秒获取一次调用,但不希望请求堆积起来

我正在尝试进行 API 调用,并且希望它每 2 秒重复一次。但是我担心如果系统在 2 秒内没有收到请求,它会建立请求并继续尝试发送它们。我怎样才能防止这种情况?

这是我正在尝试的操作fetch

const getMachineAction = async () => {
    try {
        const response = await fetch( 'https://localhost:55620/api/machine/');
        if (response.status === 200) {
            console.log("Machine successfully found.");
            const myJson = await response.json(); //extract JSON from the http response
            console.log(myJson);               
        } else {
            console.log("not a 200");
        }
    } catch (err) {
        // catches errors both in fetch and response.json
        console.log(err);
    }
};
Run Code Online (Sandbox Code Playgroud)

然后我用setInterval.

function ping() {
    setInterval(
        getMachineAction(),
        2000
    );        
}
Run Code Online (Sandbox Code Playgroud)

我曾想过在 setInterval …

javascript asynchronous fetch promise

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

使用 setTimeout 连续异步/等待

我是 ES6 的新手,所以我研究了 Javascript 的声明。

在测试 async/await 时,我发现了一些奇怪的行为。

我写了这样的代码,

const test = async () => {
    await setTimeout(() => {
        console.log("timeout");
    }, 2000);
    await console.log(1);
    await console.log(2);
}

test();
Run Code Online (Sandbox Code Playgroud)

输出在这里,

1
2
timeout
[Finished in 2.1s]
Run Code Online (Sandbox Code Playgroud)

我将 async 定义为功能并等待每一行同步工作。

预期输出在这里,

timeout
1
2
[Finished in 2.1s]
Run Code Online (Sandbox Code Playgroud)

为什么这段代码不能同步?

谢谢。

javascript async-await ecmascript-6

1
推荐指数
1
解决办法
8837
查看次数

如何在不创建 Promise 的情况下将 await 与异步函数一起使用?

我知道一个异步函数返回一个 Promise,所以我想到了替换这段代码:

const hi = function (delay) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                console.log('hi');
                resolve();
            },delay)
        });
    };

const bye = async () => {
    await hi(1000);
    await hi(1000);
    await hi(1000);
    await hi(1000);
    return 'bye';
};

bye().then((msg) => {
    console.log(msg);
});
Run Code Online (Sandbox Code Playgroud)

使用此代码:

const hi = async (delay) => {
    setTimeout(() => {
        console.log('hi');
    },delay);
};

const bye = async () => {
    await hi(1000);
    await hi(1000);
    await hi(1000);
    await hi(1000);
    return 'bye';
};

bye().then((msg) => { …
Run Code Online (Sandbox Code Playgroud)

javascript async-await

1
推荐指数
1
解决办法
65
查看次数