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

时间:2017-11-05 01:07:57

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

我正在尝试使用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 个答案:

答案 0 :(得分:0)

{/ strong> res stripe.customers.list({limit, offset}).then(res => ...)与&#34中的 res 相同;回调" stripe.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!");
    });
}

答案 1 :(得分:0)

除了Jaromanda X的答案之外,似乎客户api没有c@(unique) = $('#@(unique)phones').DataTable( { "destroy" : true "data" : existingData , "responsive": true , "searching" : false , "paging": false , "order": 0 , "createdRow" : function (row, data, index){ $(row).attr("data-id", index); rows@(unique)++; } , "columns": [ { "data": "Id" , "visible" : false } , { "data": "PhoneTypeID", "title": "Phone Type", render : function (data, type, row, meta) { // Renders combination of select element, with exisitng value correctly selected and validation control var $clone = $select.clone(); $clone.attr('name', 'Phones[' + rows@(unique) + '].PhoneTypeID' ); // changing the phone collection name for serialization $clone.find('option[value="' + data + '"]').attr('selected','selected'); // attempting to match existing data $clone.attr('data-val', 'true' ); // adding validation $clone.attr('data-val-required', 'Phone Type Required' ); // adding error message var validation = ' <div><span class="field-validation-valid text-danger" data-valmsg-for="Phones[' + rows@(unique) + '].PhoneTypeID" data-valmsg-replace="true" </span></div>'; var selectctl = $clone.wrap('<div></div>').parent().html(); // combines both the select control and the validation span element return selectctl.concat(validation); }} , { "data": "PhoneNumber", "title": "Phone Number", render : function (data, type, row) { // Renders combination of phone number text box, with exisitng value correctly selected and validation control var phoneDetail = '<div><input class="form-group" name="Phones[' + rows@(unique) + '].PhoneNumber" placeholder="Number" type="tel" pattern="\d{3}[\-]\d{3}[\-]\d{4}" value="' + data + '"' + ' data-val="true" data-val-required="Phone Required" />' + ' <input type="hidden" name="Phones[' + rows@(unique) + '].Id" value="' + row["Id"] + '" />' + ' <span class="field-validation-valid text-danger" data-valmsg-for="Phones[' + rows@(unique) + '].PhoneNumber" data-valmsg-replace="true" /></div>'; return phoneDetail; }} , { "data" : "Id", render : function (data,type,row,meta){ var deleteBtn = '<a class="btn btn-warning removeSelected" href="#">Delete</a>'; return deleteBtn; } } ] }); 选项,但offset https://stripe.com/docs/api/node#list_customers

因此,starting_after可以像

一样修复
getMoreCustomers