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

75
Views
How to modify to promise.all JS

I'm new to javascript, and I'm trying to make some changes. I have a search code in searchBar and with it a call to an API.json to fetch the information and put it on the site. Everything works fine, however I would like to modify it to add a Promise.all and be able to call more than one API and be able to display it on the site. But all my attempts have failed.

what do i have in js:

const localList = document.getElementById('localList');
const searchBar = document.getElementById('searchBar');
let hpWords = [];

searchBar.addEventListener('keyup', (e) => {
    const searchString = e.target.value.toLowerCase();

    const filteredWords = hpWords.filter((words) => {
        return (
            words.name.toLowerCase().includes(searchString)
        );
    });
    displayCharacters(filteredWords);
});

const loadCharacters = async () => {
    try {
        const res = await fetch('https://duhnunes.github.io/api/local.json');
        hpWords = await res.json();
        displayCharacters(hpWords);
    } catch (err) {
        console.error(err);
    }
};

const displayCharacters = (words) => {
    const htmlString = words
        .map((character) => {
            return `
            <div class="voc-box-content">
                <h5>${character.name} - ${character.trans} [${character.type} - ${character.type2}]</h5>
                <p>${character.description}</p>
            </div>
        `;
        })
        .join('');
        localList.innerHTML = htmlString;
};

loadCharacters();

To display on the page I am using <div id="localList"></div>.
In the example I am displaying local.json and I would also like to display item.json with <div id ="itemList"></div>.
Both have the same display structure on the <div class="voc-box-content">...</div> site.




Cheers,
DuH
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To fetch words from multiple apis you can use this:

const searchBar = document.getElementById('searchBar');
const lists = {
  localList: {
    api: 'api1'
  },
  miscList: {
    api: 'api2'
  },
  ...
};

Object.entries(lists).forEach(([id, list]) => {
  list.element = document.getElementById(id);
});

searchBar.addEventListener('keyup', (e) => {
  const searchString = e.target.value.toLowerCase();

  Object.values(lists).forEach((list) => {
    list.filteredWords = list.words.filter((word) => {
      return word.name.toLowerCase().includes(searchString);
    });
  });

  displayCharacters();
});

const loadCharacters = async () => {
  try {
    await Promise.all(
      Object.values(lists).map(async (list) => {
        const response = await fetch(list.api);
        list.words = await response.json();
      })
    );

    displayCharacters();
  } catch (err) {
    console.error(err);
  }
};

const displayCharacters = () => {
  Object.values(lists).forEach((list) => {
    list.element.innerHTML = (list.filteredWords ?? list.words)
      .map((word) => `
        <div class="voc-box-content">
          <h5>${word.name} - ${word.trans} [${word.type} - ${word.type2}]</h5>
          <p>${word.description}</p>
        </div>`)
      .join('');
  });
};

loadCharacters();
about 4 years ago · Juan Pablo Isaza 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!