【问题标题】:Fetching all stripe customers with async await使用异步等待获取所有条带客户
【发布时间】:2018-04-17 10:50:20
【问题描述】:

我正在尝试使用 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!");
  });
}

【问题讨论】:

  • 这段代码现在会发生什么...stripe.customers.list 是否偶然返回了一个承诺?
  • @JaromandaX 是的
  • promise 解析为什么值?与回调中的res 相同?
  • @user4815162342 如果它确实返回了一个承诺,那么 a) 你为什么不 return 你的函数中的承诺和 b) 你为什么仍然传递一个 nodeback?

标签: javascript node.js promise async-await ecmascript-2017


【解决方案1】:

IF res in stripe.customers.list({limit, offset}).then(res => ...)resstripe.customers.list({limit, offset}, (err, 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】:

    除了 Jaromanda X 的回答之外,客户 api 似乎没有 offset 选项,但 starting_after https://stripe.com/docs/api/node#list_customers

    所以,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!");
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2022-07-06
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 2021-07-07
      • 2014-01-25
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多