0

i am trying to reuse a function that will render contents anywhere i use it outside of it's birthplace. i have this code which fetches data from API

const showData = async function(){
   try{
       const res = await fetch(url);
       const data = await res.json();
       
       const results = data.results;
       
       const markup = results.map(elements =>{
             (.......)
         return `...html elements..`
       }).join('');

  }
  catch(error){
    console.log(error)
  }

}

thing is i wanna reuse this makrup variable in some other places as well. i tried to wrap this markup inside a function..

function generateMarkup(){
      const results....and so on until the html with return statement
  }
  generateMarkup();

as it tried to render it like this..

display.insertAdjacentHTML('afterbegin',generateMarkup);

as you guessed,it did not work,whether i close the function with parenthesis or not.

i also tried to return markup and html elements putting it inside an array.did not work either. or is there any other way u can reuse this code in some other places? can you plz help?

4

0 回答 0