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

120
Visualizações
how to pass the arguments to the call back function in catch function in javascript

I was doing the code and just felt the need ...that that I want to pass the extra argument ie msg to the callback function renderError() function along with the default error argumet which is generated by the catch function itself in the catch function

also I can't do this way .catch(renderError(err,'this is msg'));
as it will become function call and as per my knowledge We only have to pass the callback function to the higher order function rather than calling it there

  • so how can i pass it so that my msg argument

Note - Please dont refer other code as I have taken a short snippet form my code -- I just want to pass the argument to my call back function which is renderError()

const renderError = function (err, msg) {
  console.log(err);

  countriesContainer.insertAdjacentText('beforeend', msg);
  countriesContainer.style.opacity = 1;
};

const getCountryData = function (country) {
  fetch(`https://restcountries.com/v2/name/${country}`)
    .then(
      response => response.json()
      // err => console.log(err)
    )
    .then(data => {
      renderCountry(data[0]);
      const neighbour = data[0].borders[0];

      if (!neighbour) return;

      return fetch(`https://restcountries.com/v2/alpha/${neighbour}`);
    })
    .then(
      response => response.json()
      // err => console.log(err)
    )
    .then(data => renderCountry(data, 'neighbour'))
    .catch(renderError);           //  ---->> here I want to pass the message that will be 
                                   //  display along with the call back function 
};
btn.addEventListener('click', function () {
  getCountryData('germany');
});
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Wrap you renderError call in a function that exposes the error value.

.catch(error => {
  renderError(error, 'Your custom error message');
});
about 4 years ago · Santiago Gelvez Relatório
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