How do I return a different Promise only if first Promise errors out?

How do I return a different Promise only if first Promise errors out?



The specific situation is:



Maybe I'm just not understanding Promises well enough.



Here's non-working code of what I want to do, but I don't know how the syntax works.


getUserProfile()
return this.storage.get("userProfile")
.then(user =>
if (user == null)
throw new Error("no user profile");


)
.catch(error =>
//I don't know how to return a different promise
return this.getUserProfileWithHttpCall();

);


//I want to return this in getUserProfile() if "userProfile" doesn't exist in "storage"
getUserProfileWithHttpCall(): Promise < UserProfile >
return this.http.get(this.baseUrl + "/Account/GetUserInfo")
.toPromise()
.then(
response =>
this.storage.set("userProfile", response);
return response;

);



this.storage is Storage from "@ionic/storage"


this.storage



this.http is HttpClient '@angular/common/http'


this.http




1 Answer
1



For your idea, there is no need to throw any error. You can do this:


getUserProfile() this.getUserProfileWithHttpCall()
);



Or in the await, async way:


await


async


async getUserProfile()
return (await this.storage.get("userProfile"))



Maybe you want to rather use Observables, because they are the hip thing these days. you can change it to this:


Observables


getUserProfile()
return from(this.storage.get("userProfile")).pipe(
concatMap((user) => user ? of(user) : this.getUserProfileWithHttpCall())
);



You have to change your getUserProfileWithHttpCall then:


getUserProfileWithHttpCall(): Observable<UserProfile>
return this.http.get(`$this.baseUrl/Account/GetUserInfo`).pipe(
tap((user:UserProfile) => this.storage.set("userProfile", user))
)



Much neater :)



And finally, to explain why your method is not working, is because you are not returning the user in the then when it's not null, if you don't want to change your code so 'drastically', you can also just do this:


then


getUserProfile()
return this.storage.get("userProfile")
.then(user =>
if (user == null)
throw new Error("no user profile");


return user; // you missed this one

)
.catch(error =>
//I don't know how to return a different promise
// Like you already did
return this.getUserProfileWithHttpCall();

);





Thanks for the thorough answer. Though it works now, I still don't understand why you can return "user || this.getUserProfileWithHttpCall()" because user is of type UserProfile, but this.getUserProfileWithHttpCall() returns a Promise<UserProfile>
– Ambrose Leung
Sep 2 at 5:11


user


UserProfile


this.getUserProfileWithHttpCall()


Promise<UserProfile>





I wasn't able to get your 'Observable' solution to work - I think it should be getUserProfileWithHttpCall(): Observable<UserProfile > not Promise<UserProfile> (otherwise, I was getting compile errors) The compile error I get in the Observable solution of getUserProfile() is: The 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable<UserProfile>' This is more to satisfy my curiosity. I'm going to stick with what I understand. Though I really do appreciate you mentioning how it can be done with Observables
– Ambrose Leung
Sep 2 at 5:18



getUserProfileWithHttpCall(): Observable<UserProfile >


Promise<UserProfile>





If you return a promise from a promise, any consecutive then will receive the value of the nested promise.. you can chain as many as you'd like, the end result will always be the last promise in the chain. And yes, I see I made some typos last night. I've updated my answer
– PierreDuc
Sep 2 at 7:03


then





Thanks again, PierreDuc, for clarifying what happens when you return a promise in a promise
– Ambrose Leung
Sep 3 at 14:33



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)