2017-09-26 341 views
3

then()for this.getStationsAsync()从不运行。 catch()也没有,所以有可能没有被拒绝。我可以用Promise.promisify(this.getStations)做错吗?Vue/Bluebird:然后回调不运行

我也试过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) 
    } 
    } 

回答

3

您需要回报的爱可信调用,这是一个承诺的结果。

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

蓝鸟是不必要的。请致电createStationMarkers致电getStations

+0

并且我认为使用'Bluebird.promisify()'是多余的,因为'axios'也会返回一个承诺......而且它被认为是一种反实践(虽然不确定) –

+0

@ riyaz-ali我同意。 – Bert