gpt4 book ai didi

javascript - Node.js、Promises 和递归——可能吗?

转载 作者:太空宇宙 更新时间:2023-11-03 16:17:31 26 4
gpt4 key购买 nike

我想从 paypal 检索交易,直到没有更多交易可检索。我想在使用递归函数时这样做。我不知道该怎么做。可能吗?

我可以进行递归,但不能聚合事务并将它们传递出函数。

为简单起见,我删除了不相关的行。

this.retrieveAllTransactionsBetween = function(end_date, start_date) { 
return getTransactions({
STARTDATE: Moment(start_date).format().replace('+00:00', 'Z'),
ENDDATE: Moment(end_date).format().replace('+00:00', 'Z')
})
.then(function(transactions){
if (end) { // test for end of transactions
return [];
}
console.log('finish transactions in iteration where earliest was',earliest.toDate());
return transactions.concat(retrieveAllTransactionsBetween(earliest.toDate(), start_date, customer_id));
})
.catch(function(e){
console.error('getAllTransactionsBetween general error', e);
})
;
};

this.getTransactions = function(dates) {
var stringified_api_call_param = QueryString.stringify(api_call_params);

return new Promise(function(resolve, reject) {
var sequence = Sequence.create();
sequence
.then(function (next) {
api_call_options.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(stringified_api_call_param)
}
;

var req = https.request(api_call_options, paypal_helpers.paypalCallbackWrapper(next));
req.on('error', function(e) {
return reject(e.message);
});
req.write(stringified_api_call_param);
req.end();
})
.then(function (next, res){
return resolve(res);
});
});
};

最佳答案

这部分是错误的:

 return transactions.concat(retrieveAllTransactionsBetween(earliest.toDate(), start_date, customer_id));

您想等待递归调用,.concat 是一个数组函数。你应该等待它:

 return retrieveAllTransactionsBetween(earliest.toDate(), start_date, customer_id).
then(function(more){ return transactions.concat(more); });

关于javascript - Node.js、Promises 和递归——可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30006929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com