4

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.

Mango D
  • 501
  • 3
  • 11
  • 28
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Sven Aug 04 '17 at 09:56
  • How can I use the callback function in my example? – Mango D Aug 04 '17 at 11:02

2 Answers2

5

The 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);
 }
Lucas Katayama
  • 4,445
  • 27
  • 34
1

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);
});

}

Posva
  • 1,104
  • 9
  • 15
  • I get the right output then in console.log. But if I call the function somewhere else to check if it returns true or false I do not get the return in time because it is asynchronous.. How can I wait for the response? – Mango D Aug 04 '17 at 10:24
  • return the promise in `allContactsSaved` and call `.then` when you use the promise – Posva Aug 05 '17 at 12:30