Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a recursive function with a promise

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


}
like image 321
Peter3 Avatar asked Dec 23 '16 19:12

Peter3


People also ask

Can you return from a Promise?

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.

Can you return from a Promise Javascript?

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.

What does Promise () method do?

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.

What is return type of promises?

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.


2 Answers

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);
})
like image 106
forrert Avatar answered Nov 01 '22 21:11

forrert


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

        }

    })


  }
like image 35
Peter3 Avatar answered Nov 01 '22 22:11

Peter3