Answer the question
In order to leave comments, you need to log in
Why isn't destructuring assignment done without a condition?
Hello everyone, the question is rather just for reasoning and understanding zhs.
There is this code:
async function requestKeyParsingToken() {
try {
const { data } = await request({
method: "get",
url: TOKEN_SETTINGS,
})
({public_key: keyParsingTokenEndpoint} = data);
} catch (error) {
console.log(error)
}
}
if(data) {
({public_key: keyParsingTokenEndpoint} = data);
}
Answer the question
In order to leave comments, you need to log in
In this form, JS perceives the entire structure as a single expression, while the if statement contributes to the automatic placement of semicolons.
The result is the following sequence of actions:
async function requestKeyParsingToken() {
try {
({public_key: keyParsingTokenEndpoint} = data);
const {data} = await request({
method: "get",
url: TOKEN_SETTINGS,
})(data);
} catch (error) {
console.log(error)
}
}
And the error occurs due to the use of a variable before its declaration, which is prohibited with variables declared via let and const, this is called "dead float". const { data } = await request({
method: "get",
url: TOKEN_SETTINGS,
}); // точку с запятой сюда иначе следующие скобки () отработают как вызов функции. Как код отрабатывает справа на лево.
const {public_key: keyParsingTokenEndpoint} = data;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question