fetch API jsonResponse SyntaxError

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>

Upvotes: 0

Views: 37

Answers (1)

Shuvo
Shuvo

Reputation: 1293

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

} }

Upvotes: 1

Related Questions