如何使用reduce正确地链接诺言 [英] How to properly chain promises using reduce
问题描述
我有一个系统想要使用api在保管箱中创建许多文件夹,但是我似乎试图立即创建所有文件夹,这会导致生成错误,并指出我从保管箱执行了太多写操作.
I have a system that wants to create many folders in dropbox using the api, however i appear to attempt to create all the folders at once which causes errors to be generated, stating that i am performing too many write operations from dropbox.
我的代码如下,首先使用reduce来创建多个我认为被链接的promise.在这些承诺中,调用了add函数,该函数将案例上传到mongodb,然后为其创建一个保管箱文件夹,但这会导致节流错误.
My code is as follows and first uses reduce to create multiple promises which i thought were chained. In these promises the function add is called, which uploads the case to mongodb and then creates a dropbox folder for it, however this results in throttling errors..
bulkAdd: function (req, callback) {
issues = []
i = 1
req.reduce((promise, audit) => {
return promise.then(_ => this.add(audit, function(err,data){
if (err){
console.log('\n'+i+ ' ' + data.scanner_ui + '\n');
}
}));
}, Promise.resolve()).catch(error => {console.log(error)});
},
add: function (req, callback) {
delete req.status
var audit = new Audit(req);
if (req['status_value'] != undefined && req.status_value != ''){
console.log(req['status_value'])
audit.status = [{
status_value : req['status_value'],
status_notes : req['status_notes'],
status_date : req['status_date'],
}]
}
audit.save(function (err, data) {
if (err) {
callback(err, data)
}
else {
return dropbox_functions.createFolder(data.ui)
.then(response => {console.log(response)}, error=> {console.log('\n\n\n',error.error.error)})
.catch(error => console.log(error))
}
});
},
推荐答案
所以您当前问题中的问题来自于您的 add
函数不返回值的事实,我只看到它返回未定义.
So the problem in your current question comes from the fact that your add
function doesn't return a value, I only see it returning undefined.
如果一个承诺在其 then
/ catch
块中返回了承诺以外的其他内容,它将使用此输入执行以下功能,并且将不等待任何操作完成之前需要执行的内部流程
If a promise returns something else than a promise in its then
/ catch
block, it will use this input for the following function, and it will not wait for any internal processes to run through before finishing
如果在您的 then
/ catch
块中,您将返回一个promise,它将等待继续下一个 then
块,因此按顺序处理您的请求.
If inside your then
/ catch
blocks, you would return a promise, it would wait before continuing to the next then
block, thus handling your requests sequential.
现在,在您当前的代码中,我认为最简单的方法是处理reduce内的解析,因为您似乎已经被回调句柄卡住了.
Now in your current code, I believe the easiest would be to handle the resolve inside your reduce, since you seem to be stuck with your callback handle already.
req.reduce((promise, audit) => {
return promise.then(_ => new Promise(
function( resolve, reject) {
this.add(audit, function(err,data){
if (err){
console.log('\n'+i+ ' ' + data.scanner_ui + '\n');
reject( err );
return;
}
resolve( data );
});
})
);
}, Promise.resolve()).catch(error => {console.log(error)});
在这种情况下,承诺将拒绝
或解决
本身.我选择分别提交 err
和 data
,这样最终会在发生错误的情况下进行处理,这样您就可以保存最后保存的数据成功
in this case, the promise would either reject
or resolve
itself. I have chosen to submit err
and data
respectively, so this would be handled in the end in case an error occurs, and so that you can have the last data that got saved successfully
这篇关于如何使用reduce正确地链接诺言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!