Javascript 显示错误消息并在承诺中返回null
如何在控制台中显示错误并在iif的第二部分中返回值,如下所示:Javascript 显示错误消息并在承诺中返回null,javascript,promise,Javascript,Promise,如何在控制台中显示错误并在iif的第二部分中返回值,如下所示: return fetch(templatePath) .then((response) => response.ok ? response.text() : function() { console.error(`Template ${templatePath} not found.`); Promise.resolve(null); }) .then((templateHtml) => {
return fetch(templatePath)
.then((response) => response.ok ? response.text() : function() {
console.error(`Template ${templatePath} not found.`);
Promise.resolve(null);
})
.then((templateHtml) => {
newElement.innerHTML = templateHtml;
while (newElement.firstChild) {
oldElement.appendChild(newElement.firstChild);
}
})
但这会将函数显示为字符串,而不是像我希望的那样继续使用null。脚本中有2个错误: 该函数是一个匿名函数,它没有被调用,只是声明了 匿名函数未返回声明的promise.resolvenull; 解决方案: 返回fetchtemplatePath .thenresponse=>{ 如果response.ok返回response.text; 控制台。错误`Template${templatePath}未找到。`; 返回null; } 然后
脚本中有2个错误: 该函数是一个匿名函数,它没有被调用,只是声明了 匿名函数未返回声明的promise.resolvenull; 解决方案: 返回fetchtemplatePath .thenresponse=>{ 如果response.ok返回response.text; 控制台。错误`Template${templatePath}未找到。`; 返回null; } 然后 明白了 明白了
您不必创建函数,只需使用逗号运算符即可计算两个操作数并返回最后一个操作数: 返回fetchtemplatePath 然后 response=>response.ok?response.text:console.error`Template${templatePath}未找到。`,null .thentemplateHtml=>{ newElement.innerHTML=模板HTML; 而newElement.firstChild{ oldElement.appendChildnewElement.firstChild; } } 但如果发生错误,建议拒绝承诺: 返回fetchtemplatePath 然后 response=>response.ok?response.text:Promise.rejectnew错误`未找到模板${templatePath}` .thentemplateHtml=>{ newElement.innerHTML=模板HTML; 而newElement.firstChild{ oldElement.appendChildnewElement.firstChild; } }
您不必创建函数,只需使用逗号运算符即可计算两个操作数并返回最后一个操作数: 返回fetchtemplatePath 然后 response=>response.ok?response.text:console.error`Template${templatePath}未找到。`,null .thentemplateHtml=>{ newElement.innerHTML=模板HTML; 而newElement.firstChild{ oldElement.appendChildnewElement.firstChild; } } 但如果发生错误,建议拒绝承诺: 返回fetchtemplatePath 然后 response=>response.ok?response.text:Promise.rejectnew错误`未找到模板${templatePath}` .thentemplateHtml=>{ newElement.innerHTML=模板HTML; 而newElement.firstChild{ oldElement.appendChildnewElement.firstChild; } }
如果出现错误,只需返回null,而不是返回函数 然后每个块返回一个承诺。当您从传递给then函数的回调函数返回非承诺的内容时,then函数返回的承诺将在回调函数返回时解析,并立即使用回调函数返回的非承诺值来实现 在您的情况下,如果发生错误,则第一个then函数调用返回的承诺将使用null值来实现 如果传递给then函数的回调函数的返回值本身就是一个承诺,那么then函数调用返回的承诺将被解析,但尚未解决。只有当回调函数返回的承诺已解决时,它才会解决
return fetch(templatePath)
.then((response) => {
if (response.ok) return response.text();
console.error(`Template ${templatePath} not found.`);
return null;
})
.then((templateHtml) => {
newElement.innerHTML = templateHtml;
while (newElement.firstChild) {
oldElement.appendChild(newElement.firstChild);
}
})
在您的情况下,若response.ok为true,则回调函数将返回一个promise response.text。then函数调用返回的承诺将被解析,但只有在该承诺response.text被解析后才会被解析
return fetch(templatePath)
.then((response) => {
if (response.ok) return response.text();
console.error(`Template ${templatePath} not found.`);
return null;
})
.then((templateHtml) => {
newElement.innerHTML = templateHtml;
while (newElement.firstChild) {
oldElement.appendChild(newElement.firstChild);
}
})
如果出现错误,只需返回null,而不是返回函数 然后每个块返回一个承诺。当您从传递给then函数的回调函数返回非承诺的内容时,then函数返回的承诺将在回调函数返回时解析,并立即使用回调函数返回的非承诺值来实现 在您的情况下,如果发生错误,则第一个then函数调用返回的承诺将使用null值来实现 如果传递给then函数的回调函数的返回值本身就是一个承诺,那么then函数调用返回的承诺将被解析,但尚未解决。只有当回调函数返回的承诺已解决时,它才会解决
return fetch(templatePath)
.then((response) => {
if (response.ok) return response.text();
console.error(`Template ${templatePath} not found.`);
return null;
})
.then((templateHtml) => {
newElement.innerHTML = templateHtml;
while (newElement.firstChild) {
oldElement.appendChild(newElement.firstChild);
}
})
在您的情况下,若response.ok为true,则回调函数将返回一个promise response.text。then函数调用返回的承诺将被解析,但只有在该承诺response.text被解析后才会被解析
return fetch(templatePath)
.then((response) => {
if (response.ok) return response.text();
console.error(`Template ${templatePath} not found.`);
return null;
})
.then((templateHtml) => {
newElement.innerHTML = templateHtml;
while (newElement.firstChild) {
oldElement.appendChild(newElement.firstChild);
}
})
是的,就这样了,我同时知道了^^谢谢,我会在8分钟内验证你的答案,提醒我!是的,就这样了,我同时知道了^^谢谢,我会在8分钟内验证你的答案,提醒我!:哦,好的,谢谢!为什么null对r是正确的
在这儿等?我不需要回报一个承诺吗?类似于Promise.resolvenull?的东西。然后隐式地将任何不可数的值转换为已解析的承诺-就像Promise.resolve一样。所以,你不必哦,好的,谢谢!为什么null可以返回这里?我不需要回报一个承诺吗?类似于Promise.resolvenull?的东西。然后隐式地将任何不可数的值转换为已解析的承诺-就像Promise.resolve一样。所以,你不必这么做!