Promise.all处理并行请求

  async created() {
    // Promise.all([this.getTodos(), this.getComments(), this.getAlbums()]).then(
    //   (res) => {
    //     console.log(res)
    //   }
    // )
    const data = await Promise.all([
      this.getTodos(),
      this.getComments(),
      this.getAlbums()
    ])
    console.log(data)
  },
  methods: {
    getTodos() {
      return axios.get('https://jsonplaceholder.typicode.com/todos?_limit=5')
    },
    getComments() {
      return axios.get('https://jsonplaceholder.typicode.com/comments?_limit=5')
    },
    getAlbums() {
      return axios.get('https://jsonplaceholder.typicode.com/albums?_limit=5')
    }
  }

  当需要同时发送请求,所有的请求都返回结果后统一处理,可以使用Promise.all(),参数是一个数组

  返回的数据,顺序和传入的参数顺序保持一致

原文地址:https://www.cnblogs.com/wuqilang/p/15386278.html