How to make a upload message while a Promisse loads?

Asked

Viewed 100 times

2

I created a js file with the following code:


function buscarRepos(){
    lista.innerHTML = '';
    var nomeUser = document.querySelector('div#app input').value;

   var resultado = minhaPromise(nomeUser)
   .then(function(response) {
        for (let url = 0; url < response.length; url++){
            resultado = response[url].html_url;

            var itemList = document.createElement('li');
            itemList.append(resultado);

            lista.append(itemList);
        }
   })
   .catch(function(error) {
     console.warn(error);
   });

   console.log(resultado);
}

var minhaPromise = function(username){
    return new Promise(function(resolve,reject){
        var xhr = new XMLHttpRequest();
        xhr.open('GET', `https://api.github.com/users/${username}/repos`);
        xhr.send(null);

        xhr.onreadystatechange = function(){
            if (xhr.readyState == 4){
                if (xhr.status == 200){
                    resolve(JSON.parse(xhr.responseText));
                } else {
                    reject("Erro na requisição");
                }
            }
        }
    });
}

Now I need that while the result of this Promise load, a message like this appears in html:

<li>Carregando...</li>

How do I do?

1 answer

2


You can call when the function is called:

 lista.innerHTML = '<li>Carregando...</li>';
  • Thank you so much for your help!

  • I’m glad you liked the answers! But the best way to thank those who helped you is to mark "accept" the best answer and vote for all who helped you. This way you make sure that whoever wrote the answer gets something in return, as well as making the site cleaner and more useful for everyone. Adding a new answer like this (which is not an answer to the question and should be removed) makes the site more confusing and can get in the way.

  • Okay, I’m new to the site, I didn’t know about this feature. I’ll be using it from now on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.