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/