如何实现Ionic/Cordova内部的承诺

Mik*_*ike 7 cordova typescript ionic-framework ionic2 angular

我正在构建一个使用基于令牌的身份验证的Ionic应用程序.当用户授权访问令牌存储在本地存储中时.

然后,我的代码会调用提供程序以获取所需的任何数据(在这种情况下,它会从API中提取"培训程序").

反过来,该提供程序调用另一个处理我的API的HTTP请求的提供程序.它有效地设置所需的标头并启动HTTP请求,然后将响应返回给请求的提供者.

这一切都运行正常,直到我需要在发出HTTP请求之前从存储中获取访问令牌.似乎从存储访问数据不是即时的,而是使用then语法.

似乎当我从我的代码中调用提供程序时,它会传递给Training Programs提供程序,然后传递给API提供程序.这里的代码然后运行并传回,但这发生在我的应用程序有机会从存储中获取我需要的东西之前.

我尝试thenstorage.get方法的调用中将所有内容放在请求中,但这不起作用.

这是我的请求代码:

this.trainingProgramme.get_programmes().subscribe((res) => {
  loader.dismiss();
  console.log(res);
});
Run Code Online (Sandbox Code Playgroud)

哪些过滤到我的trainingProgramme提供商:

  get_programmes() {
    let seq = this.api.get('training-programmes');

    seq
      .map(res => res.json())
      .subscribe();

    return seq;
  }
Run Code Online (Sandbox Code Playgroud)

这反过来传递给我的api提供者:

export class Api {
  base_url: string = 'https://******.com';
  url: string = this.base_url + '/api/v1';      
  access_token: string;

  constructor(
    public http: Http, 
    private storage: Storage) {

    // Grab access token and store it
    storage.get('access_token').then((val) => {
      console.log('Pulled access token from local storage', val);
      this.access_token = val;
    });
  }

  // Performs a GET request with auth headers
  get(endpoint: string, params?: any) {
    let headers = new Headers();
    headers.append('Authorization', 'Bearer ' + this.access_token);

    let options = new RequestOptions({ headers: headers });

    return this.http.get(this.url + '/' + endpoint, options);
  }
}
Run Code Online (Sandbox Code Playgroud)

这不会产生任何错误,但发送的标头是:

Authorization: Bearer undefined
Run Code Online (Sandbox Code Playgroud)

总结:我认为storage调用是异步发生的,所以在有机会获取访问令牌之前启动HTTP调用.如何storage在调用HTTP请求之前防止这种情况发生或等待完成?简单地将我的代码放在里面是then行不通的,因为这个方法已经在subscribes中,所以我得到了错误.

更新1:

下面是我使用的storage代码将代码放在方法本身而不是构造函数中,然后将所有代码(包括HTTP调用)放在其中then.

  // Performs a GET request with auth headers
  get(endpoint: string, params?: any) {
    // Grab access token and store it
    this.storage.get('access_token').then((val) => {
      console.log('Pulled access token from local storage', val);
      this.access_token = val;

      let headers = new Headers();
      headers.append('Authorization', 'Bearer ' + this.access_token);

      let options = new RequestOptions({ headers: headers });    

      return this.http.get(this.url + '/' + endpoint, options);
    });
  }
Run Code Online (Sandbox Code Playgroud)

错误:

Runtime Error
Uncaught (in promise): 
TypeError: Cannot read property 'map' of undefined 
TypeError: Cannot read property 'map' of undefined at TrainingProgramme.get_programmes 
Run Code Online (Sandbox Code Playgroud)

可能这是因为该get方法在预期时不返回任何内容,因为返回现在是异步的.

Dil*_*elo 2

尝试这个 :)

yourlongmethod() {

  return new Promise((resolve, reject) => {

     //your code
     var data = "Any data you want to return with the promise";

    resolve(data);
  });
Run Code Online (Sandbox Code Playgroud)

}

然后你可以调用这个

yourlongmethod().then((res) => {

控制台.log(res) })