使用Fetch API获取数据不是异步
我有多个设备,我想通过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?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
屏幕截图显示它们正在并行运行。您可以从同一点开始屏幕截图中的绿色条。 (无论如何,最多要点:浏览器限制了飞往同一家服务器的航班中可以提出的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
.