javascript - 如何在 JavaScript 中异步发送多个请求?
问题描述
我需要一次发送 3 个请求。我得到了一些可以工作的异步功能,但会一一发送请求。哪里有问题 ?
async function fetcher(){
const cart = await send("GET", "https://www.mediaexpert.pl/cart/mini/data")
const cart_json = JSON.parse(cart)
if(cart_json.items.length === 0){
for(let item of [296752801, 299028489, 474510260]){
let json = JSON.stringify({"version": 1, "qty": 1, "gift_card_custom_price": 0, "id": item})
await send("POST", "https://www.mediaexpert.pl/cart/pre-x-add?precart=true", json)
}
return true
}
return false
}
const interval = setInterval(()=>{
fetcher().then(resp=>{
if(resp === false){clearInterval(interval)}
})
}, 500)
解决方案
您在函数中使用 async/await。所以节点将等待每个请求。如果您需要等待所有函数执行并完成,请查看Promise.all()
推荐阅读
- javascript - 单击复选框后按钮未启用
- c - C中的自定义连接
- single-sign-on - 如何在 keycloak 中为 SAML 标识提供者(onelogin)设置映射器
- bubblewrap - 初始化气泡膜
- vb.net - 用反序列化版本替换对象,并保留引用
- c++ - 如何在 std::vectors 之间进行 std::copy
当 T 有 const memers 时? - syntax - 如何运行 reghdfe?我收到语法错误
- reactjs - 如何在另一个 index.js 文件上导出 react-slick?
- html - 不需要在控制台中显示但不在 vs 代码中的元素
- c# - 错误:使用 Vue.js 在 ASP.NET Core 中请求失败,状态码为 404