如何以角度 5 返回订阅的承诺

Lal*_*wah 1 angular5

我想问一下,我们如何从 subscribe 函数返回一个 promise。

这是代码:

A.ts

makeHttpRequest() {
    return this.http.get('https://example.com/login');
}
Run Code Online (Sandbox Code Playgroud)

B.ts

class B {
    constructor(private a: A) {  
        this.a.makeHttpRequest().subscribe(data => {
            //How to return a promise from here
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

我提供了一个非常抽象的代码级别,如果有人在理解它时遇到任何问题,请告诉我。

Lal*_*wah 6

我已经开发了上述问题的解决方案:

class B {
        constructor(private a: A) {

        return new Promise((resolve,reject)=>{
         this.a.makeHttpRequest().subscribe(data => {
                resolve(true);
      }, (err)=> {
            resolve(false);
        });
  });

    }

}
Run Code Online (Sandbox Code Playgroud)