2017-09-26 340 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