Actualmente tengo un script que necesitaba obtener datos:
fetch("https://data.weather.gov.hk/weatherAPI/opendata/weather.php?dataType=flw&lang=en") .then(r => r.json()) .then(result => console.log(result))Si tuviera que continuar con el guión, puedo usar estos métodos:
Método 1:
function stuff(){ ... .then(result => function(){ <code> }) }Método 2:
function stuff(){ ... .then(result => fetchcont(result)) } function fetchcont(r){ <code> }¿Hay alguna manera de que esto se pueda presentar de una manera más ordenada, por ejemplo:
function stuff(){ ... .then(result => *continue*) <script goes here> }Gracias por adelantado
Le sugiero que use async/await en lugar de .then() .
Su código sería algo como:
// your function need to be async in order to use await keyword async function stuff(){ try { //await const res = await fetch(url); // next line will only execute when above fetch is complete const json = res.json() *Do Your other stuff here* } catch (e) { console.log("Err",e) } }