如何从 5 角订阅中返回承诺 [英] How to return a promise from subscribe in angular 5
本文介绍了如何从 5 角订阅中返回承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想问一下,我们如何从 subscribe 函数返回一个 promise.
I want to ask that how can we return a promise from the subscribe function.
代码如下:
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.
推荐答案
我已经开发了上述问题的解决方案:
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);
});
});
}
}
这篇关于如何从 5 角订阅中返回承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文