How would I get the value of the variable data
inside function myFunc()
? Below is a snippet of my code. I want to retrieve a value at this instance var data = ret;
to use outside the function console.log(data)
. The code returns an error with ReferenceError: data is not defined
.
myFunc();
function myFunc() {
query().then(function(ret) {
console.log('result', ret); // result "@dara"
var data = ret;
});
console.log(data); // ReferenceError: data is not defined
}
function query() {
var url = 'https://www.json-generator.com/api/json/get/coyqwdNpWq?indent=2';
return fetch(url, {
method: 'GET',
})
.then(function(response) {
return response.json();
})
.catch(function(error) {
console.error('Error:', error);
})
.then(function(response) {
//console.log('Success:', response[0].name);
return response[0].name;
});
}