Promises giving trouble

Asked

Viewed 42 times

-1

I’m studying JS, and I started to see about Asynchronmentalism, and I came to a class. I did as mentioned,but nothing appears in the browser network tab,nor returns error in the console,here is the code:

  var minhaPromise = function(){
    return new Promise(function(resolve, reject){
        var xhr = new XMLHttpRequest()
        xhr.open('GET', 'https://api.github.com/users/diego3g')
        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')
                }
            }
        }
    })
}

minhaPromise()
    .then(function(resolve){
        console.log(resolve)
    })
    .catch(function(error){
        console.warn(error)
    })

Then I tried to use the bibliotexa Xios,:

axios.get('https://api.github.com/user/diego3g')
.then(function(response){
    console.log(response)
})
.catch(function(error){
    console.warn(error)
})
  • I didn’t get to test with Axios, but already in its first example, it worked normally, all data from the return of the github API were displayed on the console.

1 answer

1

I circled here and it worked perfectly.

You are using some browser console, correct? As you are using console.warn in case Promise is rejected, it may be that nothing is showing up for you because your console’s Warning option is disabled. Try to enable it or change the console.warn for console.log.

Browser other questions tagged

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