本文介绍了如何从Angular 5中的订阅中返回承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想问的是,我们如何从订阅函数中返回一个承诺.
I want to ask that how can we return a promise from the subscribe function.
这是代码:
A.ts
makeHttpRequest() { return this.http.get('example/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); }); }); } }更多推荐
如何从Angular 5中的订阅中返回承诺
发布评论