在 nodejs 中使用嵌套承诺处理错误
Handing error with nested promises in nodejs
当嵌套的promise块中发生错误时,需要在最后一个外部catch块中捕获所有错误。
let docs = {
total: 0,
total_downloaded: 0,
plan_type: null,
};
Document.findAll({
where: {
report_request_id: req.params.requestId
},
attributes: ["id", "folder_name", "total_file"],
})
.then(documents => {
documents.forEach(document => {
docs.total += 1;
if (document.get("status") == 1) {
docs.total_downloaded += 1;
}
});
})
.then(function() {
Request.findOne({
where: {
id: req.params.requestId
}
})
.then(request => {
//Suppose I got error here
docs.plan_type = request.plan_type;
})
.catch(err => {
// Block A
throw err;
});
})
.then(function() {
res.status(200).send(docs);
})
.catch(err => {
// Block B
res.status(400).send(err);
});
到目前为止,即使我在 catch 块 A 中出错,我每次都能成功(200)
您可以使用 Promises 或 async/await 在一个 catch 块中捕获错误。您可以在每个 await 函数中专门抛出错误,而 await 函数又使用 promise。
有点像。
const a = new Promise((resolve, reject) => {
try {
// do something
resolve(output);
} catch (error) {
reject(error);
}
});
const b = //... similar to a();
const c = //... similar to a();
const main = async () => {
try {
const f = await a();
const i = await b(f); // if data from a is to be passed to b
const y = await c(i); // if data from a is to be passed to b
return y;
} catch(error){
// catch all errors
throw new Error( error );
}
}
main().then(( data )=>{
// on success do something with data
}).catch((error)=>{
//catch all errors and do something
});
希望对您有所帮助
您错过了 return
链接 Request.findOne()
承诺的语句。
let docs = {
total: 0,
total_downloaded: 0,
plan_type: null,
};
Document.findAll({
where: {
report_request_id: req.params.requestId
},
attributes: ["id", "folder_name", "total_file"],
})
.then(documents => {
documents.forEach(document => {
docs.total += 1;
if (document.get("status") == 1) {
docs.total_downloaded += 1;
}
});
})
.then(function() {
return Request.findOne({
where: {
id: req.params.requestId
}
})
.then(request => {
//Suppose I got error here
docs.plan_type = request.plan_type;
})
.catch(err => {
// Block A
throw err;
});
})
.then(function() {
res.status(200).send(docs);
})
.catch(err => {
// Block B
res.status(400).send(err);
});
当嵌套的promise块中发生错误时,需要在最后一个外部catch块中捕获所有错误。
let docs = {
total: 0,
total_downloaded: 0,
plan_type: null,
};
Document.findAll({
where: {
report_request_id: req.params.requestId
},
attributes: ["id", "folder_name", "total_file"],
})
.then(documents => {
documents.forEach(document => {
docs.total += 1;
if (document.get("status") == 1) {
docs.total_downloaded += 1;
}
});
})
.then(function() {
Request.findOne({
where: {
id: req.params.requestId
}
})
.then(request => {
//Suppose I got error here
docs.plan_type = request.plan_type;
})
.catch(err => {
// Block A
throw err;
});
})
.then(function() {
res.status(200).send(docs);
})
.catch(err => {
// Block B
res.status(400).send(err);
});
到目前为止,即使我在 catch 块 A 中出错,我每次都能成功(200)
您可以使用 Promises 或 async/await 在一个 catch 块中捕获错误。您可以在每个 await 函数中专门抛出错误,而 await 函数又使用 promise。 有点像。
const a = new Promise((resolve, reject) => {
try {
// do something
resolve(output);
} catch (error) {
reject(error);
}
});
const b = //... similar to a();
const c = //... similar to a();
const main = async () => {
try {
const f = await a();
const i = await b(f); // if data from a is to be passed to b
const y = await c(i); // if data from a is to be passed to b
return y;
} catch(error){
// catch all errors
throw new Error( error );
}
}
main().then(( data )=>{
// on success do something with data
}).catch((error)=>{
//catch all errors and do something
});
希望对您有所帮助
您错过了 return
链接 Request.findOne()
承诺的语句。
let docs = {
total: 0,
total_downloaded: 0,
plan_type: null,
};
Document.findAll({
where: {
report_request_id: req.params.requestId
},
attributes: ["id", "folder_name", "total_file"],
})
.then(documents => {
documents.forEach(document => {
docs.total += 1;
if (document.get("status") == 1) {
docs.total_downloaded += 1;
}
});
})
.then(function() {
return Request.findOne({
where: {
id: req.params.requestId
}
})
.then(request => {
//Suppose I got error here
docs.plan_type = request.plan_type;
})
.catch(err => {
// Block A
throw err;
});
})
.then(function() {
res.status(200).send(docs);
})
.catch(err => {
// Block B
res.status(400).send(err);
});