Hello, While my solution is working, it is a bit hard to follow and just ugly to look at. It is on Angular 5. All the 3 apis reply text. The steps are the following:
Call api1(). If successful: it returns a simple text 'ok' it will create a server side cookie. Then call api2() which makes use of the cookie created by api1() api2() returns a string that I need to save in indexdb. Furthermore, I call two other promises that do something with the result and save the result in indexdb as well. if api2() failed to return positively, i need to display a simple alert message and not call the promises at all. then call api3() which calls the backend to simply clearout the cookie it has created. That is if api1() was ok, regardless of anything that happens in between, i must always logout. The thing is though if it ends up being called before api2(), api2() replies 'anauthorized' since it can't find the cookie. Note that my application doesn't create the cookies so have no access to them. If api1() didn't succeeded, i shouldn't try to logout as the backend replies with error in that case. I hope I am making sense. So here is my approach currently which does work to some extent but have horrible flow and not capturing all error messages obviously. So can someone help me rewrite it to be more efficient, easy to follow and capture possible errors. public setUpApp(idToken): any { let vm: any = this; vm.apiService .api1(idToken) .toPromise() .then(result => { console.log("success fully authenticated and cookie created"); // result contains 'ok' return result; }) .then(data => { console.log("result from api1 ", data);// 'ok' vm.apiService .api2() .toPromise() .then(key => { if (key == null) { // the token is not attached yet alert('Token not attached to keys yet') return false; } else { // we have an key here. // call promises that process the key let promises = []; // the key is probably OK and we can save the key promises.push(this.db.setKey(key)); promises.push( this.db.createData(key) ); /* https://stackoverflow.com/questions/31424561/wait-until-all-es6-promises-complete-even-rejected-promises Basically, we want to make sure the promises are resolved either way before we return to the next */ const reflect = p => p.then( v => ({ v, status: "fulfilled" }), e => ({ e, status: "rejected" }) ); Promise.all(promises.map(reflect)).then(function(results) { var success = results.filter(x => x.status === "resolved"); }); return key; } }) .then(key => { console.log("call api3 to logout now"); vm.apiService.api3().subscribe(logOut => { console.log("logged out ", logOut); }, err=>{ // likely the cookie wasn't found so server don't know what to logout exactly console.log('error logging out from auth ', err) } ); }); }) .catch(err => { console.log("err ", err); }); } -- You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group. To unsubscribe from this group and stop receiving emails from it, send an email to angular+unsubscr...@googlegroups.com. To post to this group, send email to angular@googlegroups.com. Visit this group at https://groups.google.com/group/angular. To view this discussion on the web visit https://groups.google.com/d/msgid/angular/39ae9f52-a702-479a-beac-44f23a134b79%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.