Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

116
Vistas
await write to file only gets called randomly

I am using octokitjs to get the sha of a file and then write to the file. I have around 8 files in that Github repository. I need to write to files one by one. Here is my code:

async function separateCars() {
 await rss[0].originalItems.forEach(item => {
   const title = item.title
  //if else condition to separate car by its type
 })
}
const commitCars = async(car) => {
 const newCar = transformRssToMD(car.items);
   
 async function getSHA(path) {
   try {
     const result = await octokit.repos.getContent({
       owner: "bob",
       repo: "car-repo",
       path: path
     });
      
     const sha = result?.data?.sha;
     return sha;
  } catch (error) {
     console.log(error);
  }
}
    
function getPath(car) {
  if(car.title === 'volvo'){
     return `volvo.md`;
  } else if(car.title === 'audi'){
     return `audi.md`
  } else if(car.title === 'benz'){
     return `benz.md`
  } else if(car.title === 'bmw'){
     return `bmw.md`
  } else if(car.title === 'tesla'){
     return `tesla.md`
  } else if(car.title === 'honda'){
     return `honda.md`
  } else if(car.title === 'tata'){
     return `tata.md`
  } else if(car.title === 'suzuki'){
     return `suzuki.md`
  }
}
    
async function commitToGH(car){
  const path = getPath(car);
  const sha = await getSHA(path);

  try {
    const result = await octokit.repos.createOrUpdateFileContents({
        owner: "bob",
        repo: "car-repo",
        path: path,
        message: `latest cars updated`,
        content: Base64.encode(`${newCars}`),
        branch: 'main',
        sha,
     });
     console.log("result", result);
     return result?.status || 500;
  } catch (error) {
      console.log(error);
  }
 }
  const results = await commitToGH(car);
}


async function callCommits(){
  await separateCars().then(()=> {
     commitCars(vovlo); commitCars(tesla); commitCars(bmw);
     commitCars(tata); commitCars(suzuki); commitCars(audi);
     commitCars(honda); commitCars(benz);
   });
}

 callCommits();
}

Here the getPath gets called 8 times as expected. But getsha is only gets called randomly like 2 times in execution or 4 times in an execution. And the createorUpdateFileContents also only gets called randomly. I am struck with this problem for 2 days now. What am I doing wrong? any issue with async-await?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda