Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda