如何在promise

时间:2017-09-18 10:53:45

标签: angular ionic2 ionic3

所以我在我的离子存储器中有一个授权令牌,我必须通过http请求传递它,但我似乎无法从storage.get.then承诺中获取价值。

  getProducts(){
    let headers = new Headers(); 
    this.token= this.storage.get('Authorization').then((value)=>{
       this.token= value;
      return this.token; 
     });
     headers.append("Authorization",this.token);

    return this.http.get('https://vjtest.cobold.xyz/vapi/public/api/products',{headers: headers})
      .map(res=> res.json());

  }

3 个答案:

答案 0 :(得分:3)

由于promises是异步的,你必须等待响应,然后调用第二个请求。

select rownum as "row", to_char(date, 'YYYY') as yyyy,
       max(price) keep (dense_rank first order by date asc) as first_price,
       max(price) keep (dense_rank first order by date desc) as last_price,
       ( (max(price) keep (dense_rank first order by date desc) /
          max(price) keep (dense_rank first order by date asc)
         ) - 1
       ) as "return"
from t
group by to_char(date, 'YYYY') 
order by to_char(date, 'YYYY') ;

答案 1 :(得分:0)

您要么返回http请求(这将是一个Observable)并订阅您的函数, 或者在订阅请求本身后操纵它:

this.http
.get('https://vjtest.cobold.xyz/vapi/public/api/products',{headers: headers})
.subscribe(data => {
  //manipulate what the request returned here
});

答案 2 :(得分:0)

尝试使用async功能。

getToken() {
   return this.storage.get('Authorization');
}

async getProducts(){
  this.token = await getToken();
  let headers = new Headers(); 
  headers.append("Authorization",this.token);
  return his.http.get('https://vjtest.cobold.xyz/vapi/public/api/products',headers: headers}).map(res=> res.json());

}