我最近一直在阅读有关 async/await 并使用 try 和 catch 来处理承诺拒绝的内容,并将其应用于我的一些旧代码。
我有以下几点:
async function() {
try {
await Promise.all([some functions]);
doIfNoError();
} catch (error) {
console.log(error);
}
Run Code Online (Sandbox Code Playgroud)
我传递给 Promise.all 的函数遵循以下形式:
async function() {
some code
if (some condition) {
return true
} else {
throw false
}
}
Run Code Online (Sandbox Code Playgroud)
我打算如果任何传递给 Promise.all 的函数被拒绝,就会显示拒绝。如果没有一个函数拒绝,那么 doIfNoError 应该触发。但是,doIfNoError 有时会在不应该触发时触发,并且我会收到错误“未处理的承诺拒绝”。
我有以下演示:
#parent:nth-child(1) {
color: blue;
}
Run Code Online (Sandbox Code Playgroud)
<html>
<body>
<div id="parent">
<div>first child</div>
<div>second child</div>
<div>third child</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我期望只有父元素的第一个孩子是蓝色的,但是它们全都是蓝色。为什么是这样?
我有一个正在渲染的页面,如下所示:
\n\nres.render('account.pug', {user: req.user._id})\n
Run Code Online (Sandbox Code Playgroud)\n\n\xe2\x80\x94and 应该显示与用户相对应的个人资料图片,如下所示:
\n\nimg(id='profilepicture' src='profilepictures/#{user}')\n
Run Code Online (Sandbox Code Playgroud)\n\n然而,巴哥犬渲染为:
\n\n<img id='profilepicture' src='profilepictures/#{users}' />\n
Run Code Online (Sandbox Code Playgroud)\n\n\xe2\x80\x94 而不是:
\n\n<img id='profilepicture' src='profilepictures/USERID' />\n
Run Code Online (Sandbox Code Playgroud)\n\n\xe2\x80\x94因此不会显示正确的个人资料图片。
\n\n这很奇怪,因为当我编写div #{user}
它时,它会正确呈现为<div>USERID</div>
,所以它显然与我在属性中间字符串上进行插值有关。我尝试过使用反引号而不是引号,但这也不起作用。