Currently, I am in the process of developing a small iPhone application using React Native. My main goal is to fetch JSON data from a specific website by utilizing the fetch method. An example of my code is shown below:
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)
})
While everything seems to be working fine with fetching and handling the data, my challenge lies in storing this data for later use. Whenever I attempt to assign the fetched data to a variable within the json function, I encounter a "Request error" before eventually obtaining the data correctly. What would be the best approach to retrieve this data and store it in a variable for future usage?