2017-05-24 86 views
1

我有一個功能,在此功能,我有:離子2全局變量

this.geolocation.getCurrentPosition(posOptions).then((localisation,err) => { 

     this.storage.get('param1').then((param1) => { 

      this.storage.get('param2').then((param2) => { 
      // Do some stuff with localisation, param1 & param2 
      alert(localisation); 
      alert(param1); 
      alert(param2);    
      }) 

     }) 
    }) 

這就是我發現,在同時使用「本土化」,「參數1」 &「參數2」的唯一途徑,如果我這樣做:

this.geolocation.getCurrentPosition(posOptions).then((localisation,err) => { 
    }) 
    this.storage.get('param1').then((param1) => { 
    })    
    this.storage.get('param2').then((param2) => { 
     // Do some stuff with localisation, param1 & param2 
     alert(localisation); 
     alert(param1); 
     alert(param2);  
    }) 

它不會找到定位和參數1

回答

0

您可以使用promise.all這裏,因爲PARAMS不interdependant。 Promise.all接受一組承諾並等待所有承諾返回,然後執行then

let lPromise =this.geolocation.getCurrentPosition(posOptions); 
let p1Promise = this.storage.get('param1'); 
let p2Promise = this.storage.get('param2'); 
Promise.all([lPromise,p1Promise,p2Promise]) 
.then((values) => { 
     // Do some stuff with localisation, param1 & param2 
     alert(values[0]);//localization 
     alert(values[1]);//param1 
     alert(values[2]);//param2  
    }) 
+0

謝謝你的傢伙!有用 ! –

+0

很高興聽到它:) –