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
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');
});
Wrap you renderError call in a function that exposes the error value.
.catch(error => {
renderError(error, 'Your custom error message');
});