I have a service class called: Test
That Test
Class has a function called: getMe()
In that getMe
function, I have 3 await statements, 2nd needs the answer from the first one, third one needs an answer from the second one. An example:
class Test {
async getMe() {
const res1 = await promise1;
const res2 = await promise2(res1);
const res3 = await promise3(res2);
return res3;
}
}
Now, somewhere in my code, i call this function.
const a = new Test();
try{
const res = await a.getMe();
}catch(err){
console.log("error", err);
}
Now, because of the fact that in getMe
function, I already await promises, It's not optimized since there's a middle promise created. Read this: Difference between `return await promise` and `return promise`
So, I am wondering, If in the getMe
function, I shouldn't write await
for optimizations and return promise directly, how could my code be written ? I don't want in my outside code to call await
promise1, promise2, promise3 because then I'd not have a single function which takes care of the final bits and my code would be scattered.
What do you think and could be your advice ?