【问题标题】:Returning a recursive function with a promise返回带有承诺的递归函数
【发布时间】:2017-05-09 11:58:58
【问题描述】:

我正在尝试创建一个功能,该功能将通过端点进行分页以获取所有联系人。现在我的承诺是只返回我不明白的数字 2。我希望它返回所有联系人。这是我目前拥有的代码。我希望有人可以帮助我了解如何正确返回联系人数组。

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


}

【问题讨论】:

    标签: node.js promise


    【解决方案1】:

    我会让getContacts 函数返回一个可以解析为所有联系人列表的承诺。在该函数中,您可以链接加载数据页面的各个承诺:

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

    现在你可以像这样使用函数了:

    getContacts(myKey).then(contacts => {
        // do something with the contacts...
        console.log(contacts);
    })
    

    【讨论】:

    • forrert 我想你打败了我,但下面是我想出的结果。和你的类似。感谢您抽出宝贵时间帮助我。
    • 这是我找到的最有用的答案之一,有助于最终深入了解递归 Promise 并停止使用 explicit-construction anti-pattern
    【解决方案2】:

    这是我想出的结果。

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

    【讨论】:

    猜你喜欢
    • 2019-05-25
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2018-10-11
    • 2020-01-24
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多