I am trying to have a function that will page through an endpoint to get all of the contacts. Right now my promise is returning just the number 2 which I do not understand. I want it to return all of the contacts. Here is the code I have at the moment. I am hoping someone can help me understand how to return the array of contacts properly.
function getContacts(vid,key){
    return axios.get('https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=' + key + '&vidOffset=' + vid)
    .then(response =>{
    //console.log(response.data['has-more'])
    //console.log(response.data['vid-offset'])
    if (response.data['has-more']){
      contacts.push(getContacts(response.data['vid-offset'],key))
      if(vid === 0){
        return contacts.push(response.data.contacts)
      }else{
        return response.data.contacts   
      }
    }else{
        //console.log(contacts)
        return response.data.contacts
    }
  })
}
                Promises don't "return" values, they pass them to a callback (which you supply with . then()). It's probably trying to say that you're supposed to do resolve(someObject); inside the promise implementation. Then in your then code you can reference someObject to do what you want.
Promise resolve() method:If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.
The . promise() method returns a dynamically generated Promise that is resolved once all actions of a certain type bound to the collection, queued or not, have ended. By default, type is "fx" , which means the returned Promise is resolved when all animations of the selected elements have completed.
Returns a new Promise object that is resolved with the given value. If the value is a thenable (i.e. has a then method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise, the returned promise will be fulfilled with the value.
I would make the getContacts function return a promise that resolves to a list of all contacts. Within that function you can chain the individual promises that load a page of your data:
function getContacts(key){
    const url = 'https://api.hubapi.com/contacts/v1/lists/all/contacts/all'
    let contacts = []; // this array will contain all contacts
    const getContactsPage = offset => axios.get(
        url + '?hapikey=' + key + '&vidOffset=' + offset
    ).then(response => {
        // add the contacts of this response to the array
        contacts = contacts.concat(response.data.contacts);
        if (response.data['has-more']) {
            return getContactsPage(response.data['vid-offset']);
        } else {
            // this was the last page, return the collected contacts
            return contacts;
        }
    });
    // start by loading the first page
    return getContactsPage(0);
}
Now you can use the function like this:
getContacts(myKey).then(contacts => {
    // do something with the contacts...
    console.log(contacts);
})
                        Here is the result I came up with.
function getContacts(vid,key){
    var contacts = []
    return new Promise(function(resolve,reject){
        toCall(0)
        //need this extra fn due to recursion
        function toCall(vid){
                axios.get('https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=########-####-####-####-############&vidOffset='+vid)
                .then(response =>{
                contacts = contacts.concat(response.data.contacts)
                if (response.data['has-more']){
                  toCall(response.data['vid-offset'])      
                }else{      
                    resolve(contacts)
                }
              })
        }
    })
  }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With