简体   繁体   English

Axios 不执行并发请求

[英]Axios not performing concurrent requests

I'd like to make few requests to my backend through Vuex actions.我想通过 Vuex 操作向我的后端发出一些请求。 It would be best if those requests could be done in parallel, because they fetch data that does not depend on other requests.最好是这些请求可以并行完成,因为它们获取的数据不依赖于其他请求。

To make sure that my requests are not immediately returned, I added 3 seconds delay (server-side) to each api address to simulate long-running operations.为了确保我的请求不会立即返回,我为每个 api 地址添加了 3 秒延迟(服务器端)以模拟长时间运行的操作。

The problem I face is that requests are done sequentially and I don't quite know how to fix it.我面临的问题是请求是按顺序完成的,我不太知道如何解决它。 For two requests, there's always 6 seconds delay between 'Started' and 'Done' messages in the console, instead of ~3 seconds.对于两个请求,控制台中的“Started”和“Done”消息之间总是有 6 秒的延迟,而不是大约 3 秒。

I'm not very experienced with web development, so few of my assumptions might be incorrect, but I've already stumbled upon requirement of returning axios methods from Vuex actions, which is also what I did.我对 Web 开发不是很有经验,所以我的假设很少可能是不正确的,但我已经偶然发现了从 Vuex 操作返回 axios 方法的要求,这也是我所做的。

This is my page:这是我的页面:

<template>
  <div class="q-pa-md q-gutter-sm">
    <q-btn label="Fetch data" color="primary" flat class="q-ml-sm" @click="loadData()" />
  </div>
</template>
<script>
export default {
  methods: {
    loadData() {
      console.log('Started');

      const commonData = [
        this.$store.dispatch('currency/fetchAll'),
        this.$store.dispatch('contractor/fetchAll'),
      ];

      return Promise
        .allSettled(commonData)
        .then(() => {
          console.log('Done!');
        });
    },
  },
};
</script>

This is how my Vuex actions are implemented:这是我的 Vuex 操作的实现方式:

import { axiosInstance } from 'boot/axios';

export function fetchAll() {
  return axiosInstance
    .get('currencies/')
    .then((response) => {
      this.commit('currency/assignNewData', response.data);
    });
}
import { axiosInstance } from 'boot/axios';

export function fetchAll() {
  return axiosInstance
    .get('contractors/')
    .then((response) => {
      this.commit('contractor/assignNewData', response.data);
    });
}

Axios exposure from boot/axios.js:来自 boot/axios.js 的 Axios 暴露:

import axios from 'axios';

const axiosInstance = axios.create({
  baseURL: 'http://localhost:8080/api/rest/',
});

Vue.prototype.$axios = axiosInstance;

export { axiosInstance };

Even if I'd change calls from dispatch and replace it with axios calls like this:即使我会更改dispatch调用并将其替换为这样的 axios 调用:

const a = axiosInstance
  .get('currencies/')
  .then((response) => {
    console.log('a finished');
    this.commit('currency/assignNewData', response.data);
  });
const b = axiosInstance
  .get('contractors/')
  .then((response) => {
    console.log('b finished');
    this.commit('contractor/assignNewData', response.data);
  });
const commonData = [a,b];

it still does not make concurrent requests (I thought it might have been caused by dispatching actions to store, but it looks like it doesn't matter).它仍然没有发出并发请求(我认为这可能是由将操作分派到存储引起的,但看起来无关紧要)。

I'm using Quasar 2.1.6我正在使用 Quasar 2.1.6

I think your requests are in fact running in parallel.我觉得你的要求其实都是并行运行。 You could test that by logging Date.now() in the beginning of both your fetchAll() actions.您可以通过在两个fetchAll()操作的开头记录Date.now()来测试。

The reason you are seeing a 6-second delay between the 'Started' and 'Done' messages is because you are logging "Done" in the then() callback of Promise.allSettled() , which gets fired only after all the promises return something.您在 'Started' 和 'Done' 消息之间看到 6 秒延迟的原因是因为您在Promise.allSettled()then()回调中记录了“完成”,只有在所有承诺返回才会触发某物。

Here is the relevant excerpt from MDN about Promise.allSettled() :以下是MDN关于Promise.allSettled()的相关摘录

A pending Promise that will be asynchronously fulfilled once every promise in the specified collection of promises has completed[...]待定的 Promise 将在指定的承诺集合中的每个承诺完成后异步实现[...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM