admin管理员组文章数量:1310074
In VueJS I am trying to return a boolean with axios
allContactsSaved() {
let promise = axios.get('/contacts');
console.log(promise.then(function (response) {
response.data.data.forEach(function(contact) {
if (!contact.saved) {
return false;
}
});
return true;
}));
}
The console.log is just returning
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
But I want either true or false in return.
In VueJS I am trying to return a boolean with axios
allContactsSaved() {
let promise = axios.get('/contacts');
console.log(promise.then(function (response) {
response.data.data.forEach(function(contact) {
if (!contact.saved) {
return false;
}
});
return true;
}));
}
The console.log is just returning
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
But I want either true or false in return.
Share Improve this question asked Aug 4, 2017 at 9:54 Mango DMango D 4733 gold badges11 silver badges31 bronze badges 2- 2 Possible duplicate of How do I return the response from an asynchronous call? – Sven Commented Aug 4, 2017 at 9:56
- How can I use the callback function in my example? – Mango D Commented Aug 4, 2017 at 11:02
2 Answers
Reset to default 5The problem is not with VueJS neither Axios... I think you misunderstand Promises
Your function is asynchronous and use Promises to solve the problem, as well as axios.
To have allContactsSaved() returning with true/false to be used later, you have 3 options:
1. Promises
Return a promise, and use .then when allContactsSaved is called, like this:
// Function
// Returns promise
allContactsSaved() {
let promise = axios.get('/contacts').then(function (response) {
// check if every one is saved
const check = response.data.data.every(function(contact) {
return contact.saved;
});
return check;
}));
return promise;
}
// Using it:
allContactsSaved().then(function(isSaved) {
console.log(isSaved);
});
2. Callbacks
I think the first option is better than this one. This is kinda old school way.
// Function
// Returns promise
allContactsSaved(callback) {
axios.get('/contacts').then(function (response) {
// check if every one is saved
const check = response.data.data.every(function(contact) {
return contact.saved;
});
if(callback) {
callback(check);
}
}));
}
// Using it with function callback:
allContactsSaved(function(isSaved) {
console.log(isSaved);
});
3. Async/await
This is new for ES6/7 and depends on the version of JS engine, you will need a transpiler
// Function
// Returns promise
async allContactsSaved() {
const resp = await axios.get('/contacts');
const check = response.data.data.every(function(contact) {
return contact.saved;
});
return check;
}
// Using it, the caller function needs to be async:
async function() {
const result = await allContactsSaved();
console.log(result);
}
You can use every
to make sure every contact is saved
return response.data.ever(contact => contact.saved)
But this will still return a promise you can chain another promise:
allContactsSaved() {
let promise = axios.get('/contacts');
promise.then(function (response) {
return response.data.ever(contact => contact.saved)
}).then((areTheySaved) => {
console.log(areTheySaved);
});
}
本文标签: javascriptHow to return boolean with axiosStack Overflow
版权声明:本文标题:javascript - How to return boolean with axios - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741887963a2403134.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论