Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!