Lalit Kushwah
Lalit Kushwah

Reputation: 4049

How to return a promise from subscribe in angular 5

I want to ask that how can we return a promise from the subscribe function.

Here is the code:

A.ts

makeHttpRequest() {
    return this.http.get('https://example.com/login');
}

B.ts

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

}

I have provided a very abstract level of code if someone is facing any issue to understand it please let me know.

Upvotes: 5

Views: 5895

Answers (1)

Lalit Kushwah
Lalit Kushwah

Reputation: 4049

I have developed the solution of question asked above:

class B {
        constructor(private a: A) {

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

    }

}

Upvotes: 12

Related Questions