0

Quiero ver el json obtenido de una url en html dentro de una etiqueta con id codigo, pero al ser funcion asincrona lo que obtengo es un [object Promise] y en la consola obtengo el json.

let prueba = 'https://nominatim.openstreetmap.org/reverse?lat=41.63125237270472&lon=-4.742565007934635&format=json';

async function load() {
    let url = prueba;
    let obj = null;

    try {
        obj = await (await fetch(url)).json();
    } catch (e) {
        console.log('error');
    }

    console.log(obj);
    return obj;
}

load();
document.getElementById('codigo').innerHTML = load();;

1 respuesta 1

1

Ponlo dentro de la función así:

let prueba = 'https://nominatim.openstreetmap.org/reverse?lat=41.63125237270472&lon=-4.742565007934635&format=json';

async function load() {
    let url = prueba;
    let obj = null;

    try {
        obj = await (await fetch(url)).json();
    } catch (e) {
        console.log('error');
    }

    console.log(obj);

    jsonObj = JSON.stringify(obj);
    document.getElementById('codigo').innerHTML = jsonObj;
    
    // return obj;
}

load();
<div id="codigo">

</div>

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.