美文网首页大前端Vue
vue promise.all使用

vue promise.all使用

作者: 左木北鱼 | 来源:发表于2019-03-10 18:04 被阅读0次

问题:

一个页面可能有多个并发请求,我们想在所有请求完成后做处理,promise.all很好解决这个问题。

示例:

  created () {
    this.$toast.loading({
      duration: 0,
      forbidClick: true,
      loadingType: 'spinner'
    })
  },
  mounted () {
    this.init()
  },
  methods: {
    init () {
      Promise.all([
        this.$store.dispatch('getWeather', { city: '临沂', county: '兰山区' }),
        this.$store.dispatch('getWeather', { city: '临沂', county: '兰山区' }),
        new Promise((resolve, reject) => {
          setTimeout(() => {
            console.log(23456)
            resolve({})
          }, 3000)
        })
      ]).then(res => {
        this.$toast.clear()
        console.log(res)
      })
    }
  }
结果

相关文章

网友评论

    本文标题:vue promise.all使用

    本文链接:https://www.haomeiwen.com/subject/jakppqtx.html