Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在承诺中返回请求_Javascript_Angular_Ionic Framework_Promise_Ionic2 - Fatal编程技术网

Javascript 如何在承诺中返回请求

Javascript 如何在承诺中返回请求,javascript,angular,ionic-framework,promise,ionic2,Javascript,Angular,Ionic Framework,Promise,Ionic2,我使用的是离子2/角度2 我需要做一个http请求,但在我必须使用离子存储获取令牌之前 我为此创建了一个类apirest import {Http, Headers, RequestOptions} from '@angular/http'; import {Injectable} from '@angular/core'; import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import { S

我使用的是离子2/角度2

我需要做一个http请求,但在我必须使用离子存储获取令牌之前

我为此创建了一个类
apirest

import {Http, Headers, RequestOptions} from '@angular/http';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import { Storage } from '@ionic/storage';

@Injectable()
export class ApiRequest {

    access_token: string;

    constructor(private http: Http, public storage: Storage) {
        this.storage.get('access_token').then( (value:any) => {
            this.access_token = value;
        });
    }

    get(url) {
        let headers = new Headers({
            // 'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + this.access_token,
            'X-Requested-With': 'XMLHttpRequest'
        });
        let options = new RequestOptions({ headers: headers });

        return this.http.get(url, options)
            .map(res => res.json());

    }

}
那我就可以这样打电话了

apiRequest.get(this.URL)
        .subscribe(
            data => {
                this.element= data;
            },
            err => {
                console.log(JSON.stringify(err));
            });
我的问题是,
this.storage.get
是异步的,
http.get
也是异步的,我必须返回
http.get
,因为我想在函数外部调用
subscribe

在这种情况下,
http.get
this.acess
令牌收到值之前被调用

在这种情况下,我如何组织代码?

这可能有效(我自己没有尝试过):

这可能有效(我自己没有尝试):

这可能会有帮助~
@Injectable()
export class ApiRequest {

    access_token: string;

    constructor(private http: Http, public storage: Storage) {
        this.storagePromise = this.storage.get('access_token').then( (value:any) => {
            this.access_token = value;
        });
    }

    get(url) {
        let headers = new Headers({
            // 'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + this.access_token,
            'X-Requested-With': 'XMLHttpRequest'
        });
        let options = new RequestOptions({ headers: headers });

        return this.storagePromise.then(
           return token => this.http.get(url, options)
           .map(res => res.json());
        );
    }
}
apiRequest.get(this.URL)
    .then(observable => 
      observable.subscribe(
        data => {
            this.element= data;
        },
        err => {
            console.log(JSON.stringify(err));
        }
      );