I have these two Typescript methods:
async getCertURL(pol: string): Promise<string> {
return await Api.getData(this.apiUrl + pol + this.certEndpoint, {timeout: 60000}).then(
(response) => {
return response.data.certURL;
})
.catch((err) =>
this.loggingService.logError('Error generating reissue cert forward URL ' + err));
}
async getCert(pol: string): Promise<string> {
return Api.getData(await this.getCertURL(policy), {timeout: 60000}).then(
(response) => {
return response.data;
})
.catch((err) =>
this.loggingService.logError('Error cert not reissued ' + err));
}
I thought if I had an await
before await this.getCertURL(policy)
in my getCert()
I would not require one at Api.getData()
in getCertURL
but getCert()
throws an exception without it.
Am I correct to include it or is there some other way I should be doing this?
You need to use await
if the value on the right hand side is a promise and you want the resolved value returned instead of the promise.
Since getCertURL
is marked as async
, it will return a promise.
You need the value, so you need await
.
On the other hand, since (inside getCertURL
) you do nothing with value of the promise returned by Api.getData().then().catch()
except return it (causing it to be wrapped in a promise) you could omit the await
and async
from that function. You'd have clearer code if you replaced the then()
and catch()
with await
and try {} catch {}
though).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.