vue.js - Vue/Bluebird : then callback doesn't run

标签 vue.js vuejs2 bluebird

this.getStationsAsync() 的 then() 永远不会运行。 catch() 也没有,因此可能没有任何东西被拒绝。我可以用 Promise.promisify(this.getStations) 做错什么吗?

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

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)
    }
  }

最佳答案

您需要返回 axios 调用的结果,这是一个 promise 。

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)
    })
  }
}

Bluebird 是不必要的。只需从 createStationMarkers 调用 getStations

关于vue.js - Vue/Bluebird : then callback doesn't run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46426514/

相关文章:

vue.js - 将点击处理程序添加到引用?

vue.js - 如何绑定(bind)输入字段并同时更新 vuex 状态

node.js - 获取集合中的所有项目

javascript - 如何使用组件从 vuejs 返回一个普通数组?

javascript - 如何在 vuetify 上安装翻译?

javascript - Vue.js 加载和隐藏异步组件

node.js - Node bluebird 中的 Q.ninvoke 替换

javascript - Bluebird Promisify execFile 无法得到解决的 promise

javascript - Vue.js 中的替代绑定(bind)语法

javascript - Vue.js - 如何按特定属性对数组内的对象进行排序并使用 "v-for"渲染它