Promise在vue中的使用 Promise.all()

1、定义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、在组件中使用

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)
          }
      )
      
      
      // 使用Promise.all()方法
      let p1 = this.GLOBAL.httpPost('itArtison/user/login', params)
      Promise.all(p1).then()

https://itbilu.com/javascript…

    原文作者:zxc19890923
    原文地址: https://segmentfault.com/a/1190000015847346
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞