While working on my code, I suddenly felt the need to pass an extra argument, "msg", to the callback function renderError()
. This extra argument should be passed along with the default error argument generated by the catch function itself.
I tried doing it this way:
.catch(renderError(err,'this is msg'));
but this would turn into a function call. From my understanding, we should only pass the callback function to the higher-order function rather than calling it directly there.
- So, how can I pass the "msg" argument without breaking the rules of passing arguments?
Note - Please don't refer to other code as I have only included a small snippet from my code. I simply want to pass the argument to my callback function 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);
}; btn.addEventListener('click', function () {
getCountryData('germany');
});