如何使用mailchimp在javascript中使用两个请求来连接两个数组?

问题描述

标题可能不太清楚。我要解释我的问题。

这是我的代码

mailchimpMarketing = require("@mailchimp/mailchimp_marketing");

mailchimpMarketing.setConfig({
apiKey: "MY API KEY",server: "MY SERVER",});

const getEmailMembersFromList = async (listID,offset,count) => {
  const response = await mailchimpMarketing.lists.getListMembersInfo(listID,{offset,count});
    emails = response.members.map(member => {
      return member.email_address
    })
  console.log(emails)
}

getEmailMembersFromList("MY LIST ID",1000)
getEmailMembersFromList("MY LIST ID",1000,58)

它将在终端中显示两个电子邮件地址数组。我必须打两个电话,因为有1000多个电子邮件地址(总共1058个),最大为1000个。

现在,我想将这两个数组连接起来,也许使用.concat()方法,但是我不知道该怎么做。

编辑1

mailchimpMarketing = require("@mailchimp/mailchimp_marketing");

mailchimpMarketing.setConfig({
apiKey: "MY API KEY",count});
    emails = response.members.map(member => {
      return member.email_address
    })
  //console.log(emails)
  return emails
}

getEmailMembersFromList("MY LIST ID",58)

解决方法

尝试以下代码:

let result = [];

const getEmailMembersFromList = async (listID,offset,count) => {
  await mailchimpMarketing.lists
    .getListMembersInfo(listID,{ offset,count })
    .then((response) => {
      emails = response.members.map((member) => {
        return member.email_address;
      });
      // try using spread operator,hopefully it should work
      result = [...result,...emails];
    });
};

getEmailMembersFromList("MY LIST ID",1000);
getEmailMembersFromList("MY LIST ID",1000,58);

console.log(result);

,

由于async函数总是返回一个promise,因此您可以将所有请求添加到数组中并使用Promise.all()来获取所有数据。

仅供参考:您不会从getEmailMembersFromList函数返回任何使它正常工作所必需的东西。

示例:

const firstGroup = getEmailMembersFromList("MY LIST ID",1000);
const secondGroup = getEmailMembersFromList("MY LIST ID",58);

Promise.all([firstGroup,secondGroup]).then((values) => {
  console.log(values);
});