How can i use promise

Hey, how can i use promise with ionic 2?
With ionic 1 i had to inject the $q, but im not sure how to do that with ionic2.

From my side, I just did something like

return new Promise(resolve => {
  this.http.get('data/list.json')
    .subscribe(data => {
      this.unitsInfo = data.json();
      resolve(this.unitsInfo);
    });
1 Like

Oh man, thanks, its working but only for the success, how can i put the reject into this return?

I am not there yet, but probably something describe here

I will need to try that, that’s for sure

Yeah, this link helped a bit, i got it working now.

return new Promise(function(resolve, reject) {
  resolve('some sucess stuff');
  reject('some fail stuff');
}

Thanks for the help

Figured I’d point out the missing closing “)” in case it helps anyone else.

Should be:
> return new Promise(function(resolve, reject) {

      resolve('some success stuff');
      reject('some fail stuff');
    });

In this case I think that could be helpful to use rxjs’ toPromise operator:

this.http
.get(‘data/list.json’)
.toPromise()
.then( data => this.unitsInfo = data.json() );

1 Like

I am trying to load 3 methods inside the ionViewDidEnter but always the first method after this.carregacarrinho() does not run. Why ?

ionViewDidEnter() {
this.carregaCarrinho()
.then((data) => {
console.log(“ok”);
}, (error) => {
this.database.presentToast(“Não foi possível carregar o carrinho”);
})

this.database.buscaTipoeCliente()
.then((data) => {
this.tipovenda = data[0].tipovenda;
this.cliente = data[0].cliente;
this.codigo = data[0].codigo;
this.database.presentToast(this.tipovenda);
this.database.presentToast(this.cliente);
this.database.presentToast(this.codigo);
}, (error) => {
this.database.presentToast(“Não possível carregar Condição de venda, cliente e código”);
})

this.database.buscaUsuario()
.then((user) => {
this.usuario = user;
this.database.presentToast(this.usuario);
});
}