2017-05-24 84 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

很高兴听到它:) –