我试图搜索如何分别调用多个API。因为我所有的七个API都必须访问同一个文件。所以,它不能同时打电话。
我试着使用promise.all(),但是它不起作用。
我试着axios.then(axios.get(something).then(axios.get("something").then())));它也不起作用。
let requests = [];
requests.push(axios.post(endpoint, data));
requests.push(axios.get(`http://localhost:8080/uploadpersonfile`));
requests.push(axios.get(`http://localhost:8080/uploadcedatafile`));
requests.push(axios.get(`http://localhost:8080/uploadforenseqfile`));
requests.push(axios.get(`http://localhost:8080/uploadforenseqxfile`));
requests.push(axios.get(`http://localhost:8080/uploadforenseqyfile`));
requests.push(axios.get(`http://localhost:8080/uploadisnpsfile`));
Promise.all(requests);这是又一次尝试。
axios
.post(endpoint, data)
.then(res => {
console.log(res)
}).then(
axios.get(`http://localhost:8080/uploadpersonfile`)
.then(res => {
console.log(res)
})).then(
axios.get(`http://localhost:8080/uploadcedatafile`)
.then(res => {
console.log(res)
})).then(
axios.get(`http://localhost:8080/uploadforenseqfile`)
.then(res => {
console.log(res)
})).then(
axios.get(`http://localhost:8080/uploadforenseqxfile`)
.then(res => {
console.log(res)
})).then(
axios.get(`http://localhost:8080/uploadforenseqyfile`)
.then(res => {
console.log(res)
})).then(
axios.get(`http://localhost:8080/uploadisnpsfile`)
.then(res => {
console.log(res)
}))发布于 2019-02-06 15:04:55
Promise.all只等待多个承诺。它不关心它们的解析顺序,也不关心计算是否并行运行。
按照您的话,respectively,我假设您希望一个接一个地执行apis,而不是并行地执行
您可以尝试如下:
const executeApis = async() => {
const res1 = await axios.post(endpoint, data);
const res2 = await axios.get(`http://localhost:8080/uploadpersonfile`);
// other api hits
}发布于 2019-02-06 14:57:04
如文档所示,Promise.all将接受一系列承诺。您不需要对它们进行await,但是您需要await函数本身:
const results = await Promise.all(
axios.post(endpoint, data),
axios.get(`http://localhost:8080/uploadpersonfile`),
axios.get(`http://localhost:8080/uploadcedatafile`),
axios.get(`http://localhost:8080/uploadforenseqfile`),
axios.get(`http://localhost:8080/uploadforenseqxfile`),
axios.get(`http://localhost:8080/uploadforenseqyfile`),
axios.get(`http://localhost:8080/uploadisnpsfile`)
);现在可以在results[<promise number>]中访问查询结果。使用Promise.all将同时执行所有请求,同时保持它们的有序,从而提高性能。
发布于 2019-02-06 15:11:02
在第一个示例中,Promise.all(requests)返回一个Promise对象,而不是解析它。
尝尝这个
Promise.all(requests).then(function(res) {
console.log(response);
}).catch(function(err) {
console.error(err);
})https://stackoverflow.com/questions/54556382
复制相似问题