如何在data
内获取变量function myFunc()
的值?下面是我的代码片段。我想在此实例var data = ret;
检索一个值,以便在函数console.log(data)
之外使用。该代码返回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;
});
}
答案 0 :(得分:0)
var data = NULL或[] //将它添加到myfunc函数之外。