[英]How to know when an operation has finished - Callback?
我想知道操作何時完成-這是一個迭代,其中我們不知道要迭代的列表大小(可以是任意長度)。
這是我的代碼:
var array = [];
stripe.charges.list().autoPagingEach(function(charge) {
var post = {
chargeId: charge.id,
customerId: charge.customer,
sourceId:charge.source.id,
amount:(charge.amount/100).toFixed(2),
description:charge.description,
dateAndTime:moment(charge.created*1000).format('YYYY-MM-DD HH:mm:ss'),
chargeStatus:charge.status,
failureMessage:charge.failure_message
};
array.push(post)
});
迭代完成后,如何才能console.log(array.length)
?
我已經看到了一些使用Done()
進行回調的示例,這似乎正是我所追求的-但我無法弄清楚如何將其納入代碼中。
根據Stripe的文檔, stripe.charges.list()
返回一個帶有data
屬性中的費用列表的對象。
你可以做:
let charges = stripe.charges.list();
let pending_charges = charges.data.length;
charges.autoPagingEach(function(charge) {
// do your thing
pending_charges -= 1;
if ( pending_charges == 0 ) {
// call a function after the last charge has been processed
// and be careful with failing autoPagingEach() executions
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.