0

I have this SyntaxError after trying to implement some code from Codecademy for fetch API using jsonResponse: I think it's just that jsonResponse has to be assigned a variable cause it's a const:

https://www.accesstheflock.io/matchTable/.   PayTheBirdDog :      SyntaxError: Unexpected identifier 'await'. const declared variable 'jsonResponse' must have an initializer. l:234 c:0

<script>


/*
response.headers.get('Content-Type') + response.url + response.status

fetch('https://www.accesstheflock.io/matchTable/DoggingFistfuls')
.then(
        response => {
            alert(respons

e.json());
        },
        rejection => {
            alert(rejection.message);
        }
);
*/
const doggingFistfuls = async() => {
    try {

    fetch('https://www.accesstheflock.io/matchTable/DoggingFistfuls');

    if (response.ok) {
        const jsonResponse await response.json();
    } catch(error) {
        alert(error);
    }
    //if
}
}
</script>

1 Answer 1

1

You need to call fetch with await and assign it to a variable:

const doggingFistfuls = async() => {
try {

const response = await fetch('https://www.accesstheflock.io/matchTable/DoggingFistfuls');

if (response.ok) {
    const jsonResponse = await response.json();
} catch(error) {
    alert(error);
}
//if

} }

Sign up to request clarification or add additional context in comments.

1 Comment

I also found a wait to chain .then() with response and error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.