Can I print fetch API data with only use of one then?

Like this

fetch(signup_url, {
        method: 'POST',
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            name: user_name.value,
            email: usr_email.value,
            password: usr_password.value,
            username: user_name.value,
            lastname: usr_last_name.value,
            type: usr_type.value,
            phone: ph_number.value
        })
    })
    .then((res) => {
        var data = res.json() //Promise
        console.log(data)
    })

1 answer

  • answered 2020-11-28 12:55 Elanochecer

    You just have to await the async function:

     fetch(signup_url, {
            method: 'POST',
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({
                name: user_name.value,
                email: usr_email.value,
                password: usr_password.value,
                username: user_name.value,
                lastname: usr_last_name.value,
                type: usr_type.value,
                phone: ph_number.value
            })
        })
        .then(async (res) => {
            var data = await res.json()
            console.log(data)
        })
    

    Though i don't get why you want to use just 1 then.