I just try to understand what is happening - why my async method is waiting for another async method only if the response is deconstructed?
So I have some example code:
Dummy promise
const psedoRequest = () => {
return new Promise(resolve => setTimeout(resolve, 2000, "resolved"));
}
Dummy method which is calling promise
const methodRequest = async() => {
let response = "";
let error = "";
try {
response = await psedoRequest();
} catch (e) {
error = e;
}
return { response, error };
}
Actual methods
const invalidMainMethod = async() => {
const results = await methodRequest().response;
console.log('Invalid', results)
// the same would be with:
// const response = await methodRequest().response;
// console.log('Invalid', response );
}
const validMainMethod = async() => {
let results = ""
const { response } = await methodRequest();
results = response;
console.log('Valid', results);
}
Console log returns:
Invalid undefined
Valid resolved
Why deconstructing actually works in that case - so it's waiting for a response while accessing directly .response
is not?
I thought that deconstructing is some syntactic sugar.