使用Fetch API获取数据不是异步

发布于 2025-02-13 03:35:06 字数 848 浏览 0 评论 0原文

我有多个设备,我想通过API发射命令 但是使用获取无法正常工作。 如您在图片中看到的那样,9个请求大约需要一分钟才能完成,因为执行时间约为每个请求6秒。如果我解雇命令,它们都出现在开发控制台中,但是“待定”为什么这不起作用?

async function getExecuteOutput(devices, formData) {

    $.each(devices, async function( index, value ) {
        fetch(
            '/api/execute.php',
            {
                method: 'POST',
                body: foormData
            }
        ).then((resp) => resp.json()).then(response => {
            console.log(response);
        });
    });
}

i have multiple devices which i want to fire a command over a api
But using fetch is not working as expected.
As you can see in the picture, 9 requests takes about a minute to finish, because the execution takes about 6 seconds per request. If i fire the command, they all appear in the dev console, but "pending" Why this is not working?

enter image description here

enter image description here

async function getExecuteOutput(devices, formData) {

    $.each(devices, async function( index, value ) {
        fetch(
            '/api/execute.php',
            {
                method: 'POST',
                body: foormData
            }
        ).then((resp) => resp.json()).then(response => {
            console.log(response);
        });
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯寂 2025-02-20 03:35:07

屏幕截图显示它们正在并行运行。您可以从同一点开始屏幕截图中的绿色条。 (无论如何,最多要点:浏览器限制了飞往同一家服务器的航班中可以提出的HTTP请求的数量)。

您的服务器端代码无法处理(可能是由于数据库锁定)与fetch无关。

The screenshot shows they are running in parallel. You can see the green bars in the screenshot starting at the same point. (Up to a point anyway: browsers limit the number of HTTP requests they can have in flight to the same server).

Your server-side code's inability to handle that (probably due to database locking) has nothing to do with fetch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文