【发布时间】:2022-01-27 11:33:15
【问题描述】:
我正在从 XML API 检索数据。为了将结果呈现到屏幕上,我必须调用一个端点来获取数据列表,然后我必须映射项目列表并为每个返回的项目调用第二个端点。第一个请求成功通过;但是其余的请求似乎是同时调用的。这导致大约 90% 的请求返回以下错误:
Maximum allowed concurrent requests threshold(10) was breached
我调用 API 端点的后端函数将 XML 作为字符串接收,并将响应转换为 JSON。代码如下:
export function newSoapRequest(options = {
method: "POST",
url: "",
headers: {},
xml: "",
timeout: 10000
}) {
const {
method,
url,
headers,
xml, // soap envelope as string
timeout
} = options
return new Promise((resolve, reject) => {
axios({
method: method || "POST",
url,
headers,
data: xml,
timeout
}).then((res) => {
resolve({
res: {
headers: res.headers,
body: XML.parse(res.data),
statusCode: res.status,
}
})
}).catch((err) => {
if(err.response) {
reject(err.response.data)
} else {
reject(err)
}
})
})
}
这是我从地图函数调用 API 的代码
resortList.map(async (resort) => {
let resortXML = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dae="DAE"><soapenv:Header/><soapenv:Body><dae:DAEGetResortProfile><!--Optional:--><dae:AuthID>GWAYUAT</dae:AuthID><!--Optional:--><dae:EndpointID>${resort.WeekEndpointID}</dae:EndpointID><!--Optional:--><dae:ResortID>${resort.ResortId}</dae:ResortID></dae:DAEGetResortProfile></soapenv:Body></soapenv:Envelope>`
await newSoapRequest({method: "POST", url, headers: sampleHeaders, xml: resortXML, timeout: 10000})
.then(response => console.log(response))
.catch(err => console.log(err))
})
据我所知,响应是从 newSoapRequest 函数发送的,然后连接关闭。当我在 map 函数中使用 async await 时,map 函数应该等到请求完成并关闭连接,然后再处理响应,然后第二次运行。这个问题可能与它本身的地图功能有关吗?还是我的 newSoapRequest 函数没有正确关闭连接?
【问题讨论】:
标签: javascript post axios xmlhttprequest