tengo un problema con mi código, todo lo que necesito es guardar los resultados del bucle api en el archivo json usando writeFileSync. Aquí está mi código:
function processRarity(limit) { var resultJson = []; for (var i = limit; i < limit + 100; i++) { const everything = async () => { const response = await fetch( "https://api-v2-mainnet.paras.id/token/asac.near::" + i ); var json = await response.json(); return json; }; everything().then((res) => { console.log(res); resultJson = res; }); } fs.writeFileSync(`${basePath}/results.json`, JSON.stringify(resultJson, null, 2)); }cuando reviso mi archivo json, todo lo que obtengo es solo:
[]Debe await las llamadas a everything() .
Además, debe presionar resultJson , no reasignarlo cada vez que pasa por el ciclo.
const everything = async (i) => { const response = await fetch( "https://api-v2-mainnet.paras.id/token/asac.near::" + i ); var json = await response.json(); return json; }; async function processRarity(limit) { var resultJson = []; for (var i = limit; i < limit + 100; i++) { let res = await everything(i); console.log(res); resultJson.push(res); } fs.writeFileSync(`${basePath}/results.json`, JSON.stringify(resultJson, null, 2)); }