平展承诺链并执行特定于错误的catch语句
Flatten out promise chain and execute error specific catch statements
我在将我的承诺作为嵌套的替代方案时遇到了一点麻烦。
假设我想通过检索用户并使用其id检索另一个表中的相关帖子来进行表连接。一样。
User.get(uid)
.then(usr => {
return Post.get(usr.id)
})
.then(posts => {
//send posts or continue the promise chain
});
现在,每当我想执行特定于某个承诺的错误处理逻辑而不执行其他任何内容时,问题就出现了。在我的例子中,如果数据库没有找到具有该ID的用户,它会抛出一个错误,然后如果它没有通过给定的外键找到帖子,它也会抛出一个错误。我想做的是响应一个特定的错误消息,如"未找到用户"或"给定用户未找到帖子"等。
I tried to do this
User.get(uid)
.catch(e => {
//This executes whenever a user is not found but the problem is that it also
//executes the following 'then' statement
})
.then(usr => {
return Post.get(usr.id)
})
.then(posts => {
//send posts or continue the promise chain
})
.catch(e => {
//This should only execute when posts aren't found with the id
});
现在前面的代码不起作用了,因为.then
不考虑错误而执行。
所以我考虑删除catch语句之后的所有。then语句,就像这样
User.get(uid)
.then(usr => {
return Post.get(usr.id)
})
.then(posts => {
//send posts or continue the promise chain
})
.catch(e => {
//This executes whenever a user is not found but the problem is that it also
//executes the following 'then' statement
})
.catch(e => {
//This should only execute when posts aren't found with the id
});
但是这不起作用,因为第一个。catch语句总是执行。
在同步方式下,这将是我编写代码的方式
try
{
var user = getUser(id);
try
{
var posts = getPosts(user.id);
}
catch(e)
{
//this executes only if posts aren't found
}
}
catch (e)
{
//this executes if the error originates from obtaining a user
}
问题的核心是:
我想做的是响应一个特定的错误消息,如"用户未找到"或"给定用户未找到帖子"等。
两件事:
- 错误对象可以通过附加属性进行扩展。
- 你不局限于接球-你也可以扔/再扔。
你可以写一个平链:
User.get(uid)
.catch(e => { // initial catch is simple
e.userMessage = "problem finding user"; // add user message as a custom property
throw e; // rethrow the augmented e to ensure the rest of the success path is not executed
})
.then(usr => Post.get(usr.id))
.catch((e) => { // intermediate catch(es) follow this pattern
if(!e.userMessage) {
e.userMessage = "problem finding posts"; // add user message as a custom property
}
throw e; // rethrow the augmented e to ensure the rest of the success path is not executed
})
.then(posts => send(posts))
.catch(e => { // terminal catch is a bit different
console.log(e); // (optional)
if(e.userMessage) {
display(e.userMessage);
} else {
display("problem sending pots");
}
});
但是需要在每个阶段区分最新的|较早的错误,使得这个平面模式稍微麻烦一些。
这样更简单,更符合承诺的精神,而不是使之变平。相反,让每个阶段都有自己的"私有"捕获。在第一阶段之后,这意味着嵌套捕获。
User.get(uid)
.catch(e => {
e.userMessage = "problem finding user"; // add user message as a custom property
throw e; // rethrow the augmented e to ensure the rest of the success path is not executed
})
.then(usr => Post.get(usr.id).catch(e => {
e.userMessage = "problem finding posts"; // add user message as a custom property
throw e; // rethrow the augmented e to ensure the rest of the success path is not executed
}))
.then(posts => send(posts).catch(e => {
e.userMessage = "problem sending pots"; // add user message as a custom property
throw e; // rethrow the augmented e
}))
.catch(e => {
console.log(e); // (optional) log the error with its .message and .userMessage
display(e.userMessage);
});
无论哪种方式,都会为三个可能的来源中的每一个产生的错误提供自定义用户消息。
相关文章:
- 将错误与if语句混淆
- 如何知道javascript for语句中的所有结果是否都是错误的
- MVC Jquery-按钮点击-获取if语句错误
- 由 if / else 语句中的无效语法导致的语法错误
- Javascript else-if语句错误
- 阻止表单提交 javascript 为什么有一个明显的错误语句返回 true
- 筛选器后的行计数错误(使用if语句)
- Simple Javascript if语句中存在语法错误
- Null记录错误地触发Else语句
- Javascript if else语句OR条件错误
- 如果语句返回错误答案
- 防止 if 语句中的错误“未定义”
- 使用 If else if else 语句得到错误
- ES6 大箭头符号函数给出语法错误:缺失;在语句之前
- 非法返回语句错误
- 为什么我的 if 语句出现语法错误
- 为什么以下语句执行没有错误
- 我的“if”语句中包含中断的错误(wrox ch 3 练习 4)
- 为什么在调用丢失的函数时会捕获错误,而不是在错误语句中捕获错误
- jQuery无线电检查命中错误语句