I have this function:
private async get(iD: string): Promise<Function> {
return async () => {
const tallConfig = await longCrudService.getHalf(iD);
const stream: Stream = tallConfig.stream;
const response = this.createUrl(tallConfig, stream);
return response;
};
}
Is there a way I could re-write this but without using double async, like is there a way I could re-write this by using only the async of the function, and removing the inner async?
CodePudding user response:
The only decent use of async
is when you need to await
directly inside. Since you aren't doing so for the outer function, you can remove it - only the inner function is making use of an await
.
private get(iD: string): Promise <Function> => async () => {
const tallConfig = await longCrudService.getHalf(iD);
const stream: Stream = tallConfig.stream;
const response = this.createUrl(tallConfig, stream);
return response;
}
You could also remove the async
s entirely if you wanted, in exchange for .then
.
private get(iD: string): Promise <Function> => () => longCrudService.getHalf(iD)
.then(tallConfig => this.createUrl(tallConfig, tallConfig.stream));
CodePudding user response:
There's no need for an inner function you can rewrite as such:
private async get(iD: string): Promise<Function> {
const tallConfig = await longCrudService.getHalf(iD);
const stream: Stream = tallConfig.stream;
return this.createUrl(tallConfig, stream);
}
Make sure that the caller does not need another function call as this would return a promise as specified on your function signature.