Home  >  Article  >  Web Front-end  >  How to use Promise object Promise.all() method in Vue? (Pure code)

How to use Promise object Promise.all() method in Vue? (Pure code)

不言
不言Original
2018-08-01 16:49:337661browse

This article introduces to you how to use the Promise.all() method of the Promise object in Vue? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Define promise

<script type="text/javascript">
// 定义一些公共的属性和方法
const httpUrl = 'http://39.105.17.99:8080/'
function promiseFun (url, params) {
  return new Promise((resolve, reject) => {
    this.$http.post(this.globalHttpUrl + url, params).then(
      (res) => {
        resolve(res.json())
      },
      (err) => {
        reject(err.json())
      }
    )
  })
}
var p1 = Promise.resolve(1)
var p2 = Promise.resolve(2)
var p3 = Promise.resolve(3)
Promise.all([promiseFun, p1, p2, p3]).then((res) => {
  console.log(res, 'promise all 方法')
})
// 暴露出这些属性和方法
export default {
  httpUrl,
  promiseFun
}
</script>

2. Use it in components

methods: {
    loginInFun () {
      localStorage.setItem('userId', '00001')
      let params = {
        telphone: this.username,
        password: this.password
      }
      let pro1 = this.promiseFun('itArtison/user/login', params)
      let pro2 = this.promiseFun('itArtisOn/user/register', params1)
      
      // 使用 Promise.all()
      Promise.all([pro1, pro2]).then(
          (res) => {
              console.log(res);
          }
      ).catch(
          (err) => {
              console.log(err)
          }
      )

Recommended related articles:

Usage of BOM objects in Javascript

What are the usages of call() method and apply() method in Javascript? (Code attached)

The above is the detailed content of How to use Promise object Promise.all() method in Vue? (Pure code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn