使用异步等待获取所有条带客户

时间:2022-02-10 21:52:18

I am trying to compile a list of all customers using the Stripe Node API. I need to make continual fetches 100 customers at a time. I believe that I need to use a Promise within the API call to use async await, but I can't for the life of me figure out where to put it. Hoping to make this gist public use and I want to get it right, thanks.

我正在尝试使用Stripe Node API编译所有客户的列表。我需要一次连续吸引100个客户。我相信我需要在API调用中使用Promise来使用异步等待,但我不能在我的生活中找出放在哪里。希望公开使用这个要点,我想要做对,谢谢。

getAllCustomers()

function getMoreCustomers(customers, offset, moreCustomers, limit) {
  if (moreCustomers) {
    stripe.customers.list({limit, offset},
      (err, res) => {
        offset += res.data.length
        moreCustomers = res.has_more
        customers.push(res.data)
        return getMoreCustomers(customers, offset, moreCustomers, limit)
      }
    )
  }
  return customers
}

async function getAllCustomers() {
  const customers = await getMoreCustomers([], 0, true, 100)
  const content = JSON.stringify(customers)
  fs.writeFile("/data/stripe-customers.json", content, 'utf8', function (err) {
      if (err) {
          return console.log(err);
      }
      console.log("The file was saved!");
  });
}

2 个解决方案

#1


0  

IF res in stripe.customers.list({limit, offset}).then(res => ...) is the same as res in the "callback" version of stripe.customers.list({limit, offset}, (err, res) - then you probably could rewrite your code like

IF res在stripe.customers.list({limit,offset})中。然后(res => ...)与res.c在3.“的回调”版本中与stripe.customers.list({limit,offset},(错误,res) - 然后你可能会重写你的代码

const getMoreCustomers = limit => {
    const getThem = offset => stripe.customers.list({limit, offset})
    .then(res => res.has_more ? 
        getThem(offset + res.data.length).then(result => res.data.concat(...result)) : 
        res.data
    );
    return getThem(0);
};

async function getAllCustomers() {
    const customers = await getMoreCustomers(100);
    const content = JSON.stringify(customers);
    fs.writeFile("/data/stripe-customers.json", content, 'utf8', function (err) {
        if (err) {
            return console.log(err);
        }
        console.log("The file was saved!");
    });
}

#2


0  

additional to Jaromanda X's answer, it seems there is no offset option to customers api, but starting_after https://stripe.com/docs/api/node#list_customers

除了Jaromanda X的答案之外,似乎客户api没有偏移选项,但是起始_后https://stripe.com/docs/api/node#list_customers

So, getMoreCustomers may be fixed like

因此,getMoreCustomers可能会被修复

const getMoreCustomers = starting_after => {
  const getThem = starting_after => stripe.customers.list({limit: 100, starting_after: starting_after})
  .then(res => res.has_more ? 
      getThem(res.data[res.data.length - 1]).then(result => res.data.concat(...result)) : 
      res.data
  );
  return getThem(starting_after);
};

async function getAllCustomers() {
  const customers = await getMoreCustomers(null);
  const content = JSON.stringify(customers);
  fs.writeFile("/data/stripe-customers.json", content, 'utf8', function (err) {
    if (err) {
      return console.log(err);
    }
    console.log("The file was saved!");
  });
}

#1


0  

IF res in stripe.customers.list({limit, offset}).then(res => ...) is the same as res in the "callback" version of stripe.customers.list({limit, offset}, (err, res) - then you probably could rewrite your code like

IF res在stripe.customers.list({limit,offset})中。然后(res => ...)与res.c在3.“的回调”版本中与stripe.customers.list({limit,offset},(错误,res) - 然后你可能会重写你的代码

const getMoreCustomers = limit => {
    const getThem = offset => stripe.customers.list({limit, offset})
    .then(res => res.has_more ? 
        getThem(offset + res.data.length).then(result => res.data.concat(...result)) : 
        res.data
    );
    return getThem(0);
};

async function getAllCustomers() {
    const customers = await getMoreCustomers(100);
    const content = JSON.stringify(customers);
    fs.writeFile("/data/stripe-customers.json", content, 'utf8', function (err) {
        if (err) {
            return console.log(err);
        }
        console.log("The file was saved!");
    });
}

#2


0  

additional to Jaromanda X's answer, it seems there is no offset option to customers api, but starting_after https://stripe.com/docs/api/node#list_customers

除了Jaromanda X的答案之外,似乎客户api没有偏移选项,但是起始_后https://stripe.com/docs/api/node#list_customers

So, getMoreCustomers may be fixed like

因此,getMoreCustomers可能会被修复

const getMoreCustomers = starting_after => {
  const getThem = starting_after => stripe.customers.list({limit: 100, starting_after: starting_after})
  .then(res => res.has_more ? 
      getThem(res.data[res.data.length - 1]).then(result => res.data.concat(...result)) : 
      res.data
  );
  return getThem(starting_after);
};

async function getAllCustomers() {
  const customers = await getMoreCustomers(null);
  const content = JSON.stringify(customers);
  fs.writeFile("/data/stripe-customers.json", content, 'utf8', function (err) {
    if (err) {
      return console.log(err);
    }
    console.log("The file was saved!");
  });
}