fetch('http://jsonplaceholder.typicode.com/users/5',
{
method: 'get'
})
.then(function (pending)
{
return pending.json(); //returning response as json after completion of async call, you can use .text() instead to return response as text
})
.then(function (response)
{
console.log(response); //recieved GET response
})
.catch(function (error)
{
console.log(error); //Handle any error here
});This code snippet for Async GET Call using Fetch API in JavaScript.
Fetch API is replacement for XmlHttpRequest with simple syntax returning Promise object which makes async call chaining simpler. Here examples is showing GET call using fetch, you can use other HTTP verbs as well.
