I'm new to Angular so it's still rather difficult for me to write code in an efficient way. I've recently come upon this problem regarding Promise chaining and I'm wondering if there is a better way to go at it.
The gist is like this: I have function A, which needs result from function B. If I get a certain result from B, Then I would call C. B and C both returns Promises.
So in the end I got a very ugly block of code like this.
funcA(): Promise<MyObj> {
return new Promise(function(resolve, reject) {
funcB().then (res => {
check if res satisfies condition
funcC().then( obj => {
process obj
resolve(obj)
}, reason => doSomething then reject)
}, reason => doSomething then reject)
}
}
The code works and serves my need, but it's difficult to read and I think there might be much better way to do this. The problem gets worse since there are a lot of places that I need to make similar call and sometimes the chaining might be even more complicated. So if anyone can provide a suggestion of how to better reformat my code or to how to rewrite function with Promise chaining I would greatly appreciate it.