I'm writing small app for iPhone using react native. I'm trying to fetch JSON data from a website using fetch. Like in the example:
function status(response) {
if (response.status >= 200 && response.status < 300) {
return response
}
throw new Error(response.statusText)
}
function json(response) {
return response.json()
}
fetch('/users', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'Hubot',
login: 'hubot',
})
}).then(status)
.then(json)
.then(function(json) {
console.log('request succeeded with json response', json)
}).catch(function(error) {
console.log('request failed', error)
})
Everything works fine but I need to use that data later. When I try to assign it to some variable in json function, I get "Request error" and after that get the data (correctly). What's the best practise to get that data and have it in some variable to use it later?