Vue / Bluebird:然后回调没有运行

时间:2017-09-26 12:24:51

标签: vue.js vuejs2 bluebird

this.getStationsAsync()的then()永远不会运行。 catch()也没有,所以很可能没有被拒绝。我可以对Promise.promisify(this.getStations)做错了吗?

我还在this.getStationsAsync = Promise.promisify(this.getStations)挂钩中尝试了created()。我没有收到任何错误,但也没有收到任何console.logs()表示已执行then()

import Vue from 'vue'
import axios from 'axios'
import Promise from 'bluebird'

methods:{
createStationMarkers (selectedNetworkMarker) {
      this.selectedNetwork = selectedNetworkMarker
      this.hideNetworkMarkers()
      debugger
      this.getStationsAsync()
      .then(() => {
        debugger
        console.log('inside then')
        this.addStationMarkers()
        this.test = true
      })
      .catch(error => {
        console.log(error)
      })
    }
},
getStations () {
      axios
        .get('/api/network/' + this.selectedNetwork.id)
        .then(res => {
          for (let station of res.data.stations) {
            this.stations.push(station)
          }
          return res.data.stations
        })
        .catch(error => {
          console.log(error)
        })
    }
}

computed: {
    getStationsAsync () {
      return Promise.promisify(this.getStations)
    }
  }

1 个答案:

答案 0 :(得分:3)

你需要返回 axios调用的结果,这是一个承诺。

getStations () {
  return axios
    .get('/api/network/' + this.selectedNetwork.id)
    .then(res => {
      for (let station of res.data.stations) {
        this.stations.push(station)
      }
      return res.data.stations
    })
    .catch(error => {
      console.log(error)
    })
  }
}

蓝鸟是不成功的。只需从getStations致电createStationMarkers