首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用axios分别调用7个api

如何使用axios分别调用7个api
EN

Stack Overflow用户
提问于 2019-02-06 14:52:07
回答 3查看 52关注 0票数 0

我试图搜索如何分别调用多个API。因为我所有的七个API都必须访问同一个文件。所以,它不能同时打电话。

我试着使用promise.all(),但是它不起作用。

我试着axios.then(axios.get(something).then(axios.get("something").then())));它也不起作用。

代码语言:javascript
复制
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);

这是又一次尝试。

代码语言:javascript
复制
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)
      }))
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-02-06 15:04:55

Promise.all只等待多个承诺。它不关心它们的解析顺序,也不关心计算是否并行运行。

按照您的话,respectively,我假设您希望一个接一个地执行apis,而不是并行地执行

您可以尝试如下:

代码语言:javascript
复制
const executeApis = async() => {
  const res1 = await axios.post(endpoint, data);
  const res2 = await axios.get(`http://localhost:8080/uploadpersonfile`);
  // other api hits 
}
票数 -1
EN

Stack Overflow用户

发布于 2019-02-06 14:57:04

如文档所示,Promise.all将接受一系列承诺。您不需要对它们进行await,但是您需要await函数本身:

代码语言:javascript
复制
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将同时执行所有请求,同时保持它们的有序,从而提高性能。

票数 0
EN

Stack Overflow用户

发布于 2019-02-06 15:11:02

在第一个示例中,Promise.all(requests)返回一个Promise对象,而不是解析它。

尝尝这个

代码语言:javascript
复制
Promise.all(requests).then(function(res) {
 console.log(response);
}).catch(function(err) {
  console.error(err);
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54556382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档